jQuery实现一个简单的验证码功能
2017-06-26 09:47
761 查看
在学习jQuery过程中,写的一个简单的验证码的小例子,记载下来,方便以后借鉴补充,源码如下:
<!DOCTYPE html> <html> <head> <title></title> <style type="text/css"> div{ background-color:blue; width:200px; height:100px; font-size:35px; } </style> <script src="../jquery-1.8.0.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { //我写的验证码 //验证码 var code; function createCode(){ code = '';//首先默认code为空字符串 var codeLength = 4;//设置长度,这里看需求,我这里设置了4 var codeV = $("div"); //设置随机字符 var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', 'S','T','U','V','W','X','Y','Z'); for(var i = 0; i < codeLength; i++){ //循环codeLength 我设置的4就是循环4次 var index = Math.floor(Math.random()*36); //设置随机数范围,这设置为0 ~ 36 code += random[index]; //字符串拼接 将每次随机的字符 进行拼接 } codeV.text(code);//将拼接好的字符串赋值给展示的Value } //页面开始加载验证码 createCode(); //验证码Div加载点击事件 $("div").bind('click',function() { createCode(); }); //下面就是判断是否==的代码,无需解释 $("#b1").bind('click',function() { var oValue = $("#in1").val().toUpperCase(); $("#l1").html(""); if(oValue ==""){ $("#l1").html("<font color='red'>请输入验证码</font>"); }else if(oValue != code){ $("#l1").html("<font color='red'>验证码不正确,请重新输入</font>"); oValue = ""; createCode(); }else{ $("#l1").html("<font color='blue'>验证码正确</font>"); } }); }); </script> </head> <body> <center> <label >请输入验证码:</label><input type="text" id="in1" value="" placeholder="请输入验证码"> <button id="b1">点击验证</button> <div></div><label id="l1"></label> </center> </body> </html>
您可能感兴趣的文章:
相关文章推荐
- 一个简单的jQuery计算器实现了连续计算功能
- 一个简单的jQuery计算器实现了连续计算功能
- 利用jQuery 实现一个简单的侧边菜单隐藏显示功能
- jQuery与vue分别实现超级简单的绿色拖动验证码功能
- 用JQuery写的一个简单的验证码功能
- input 点击放大镜背景搜索、jQuery实现简单前端搜索功能
- 一个简单求和函数的matlab实现(带程序耗时功能)
- jQuery实现验证码功能
- 使用jQuery实现的简单滚动新闻功能
- 使用UIScrollView和UIPageControl实现一个简单的图片浏览功能
- 用最简单的函数实现功能:判断一个int数据是否是2的x次幂(不能使用循环)。
- 一个简单的关于计算器功能的实现(剽窃)
- 简单的angular实现一个购物车小功能
- 一个简单的jQuery插件ajaxfileupload.js实现ajax上传文件例子
- 利用jQuery实现一个简单的表格上下翻页效果
- 转:一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
- jQuery数据缓存功能的解析及简单实现
- JavaScript操作CSS样式实现简单的验证码的功能
- JQuery实现简单验证码提示解决方案
- 一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子