Jquery 客户端生成验证码

验证码的作用:

  1.有效防止这种问题对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试,实际上是用验证码是现在很多网站通行的方式(比如招商银行的网上个人银行,腾讯的QQ社区),我们利用比较简易的方式实现了这个功能。

  2.防止批量注册

首先要准备jquery、jquery.idcode.css 和 jquery.idcode.js 三个脚本,然后再html文档中进行实例生成。

jquery.idcode.css 样式:
Jquery 客户端生成验证码Jquery 客户端生成验证码
 1 @charset "utf-8";
 2 /* track base Css */
 3  
 4 .ehong-idcode-val{
 5     position:relative;
 6     padding:1px 4px 1px 4px;
 7     top:0px;
 8     *top:-3px;
 9     letter-spacing:4px;
10     display:inline;
11     cursor:pointer;
12     font-size:20px;
13     font-family:"Courier New", Courier, monospace;
14     text-decoration:none;
15     font-weight:bold;
16 }
17 .ehong-idcode-val0{
18     border:solid 1px #A4CDED;    
19     background-color:#ECFAFB;
20 }
21 
22 .ehong-idcode-val1{
23     border:solid 1px #A4CDED;    
24     background-color:#FCEFCF;
25 }
26 .ehong-idcode-val2{
27     border:solid 1px #6C9;    
28     background-color:#D0F0DF;
29 }
30 .ehong-idcode-val3{
31     border:solid 1px #6C9;    
32     background-color:#DCDDD8;
33 }
34 .ehong-idcode-val4{
35     border:solid 1px #6C9;    
36     background-color:#F1DEFF;
37 }
38 .ehong-idcode-val5{
39     border:solid 1px #6C9;    
40     background-color:#ACE1F1;
41 }
42 .ehong-code-val-tip{
43     font-size:12px;
44     color:#1098EC;
45     top:0px;
46     *top:-3px;    
47     position:relative;    
48     margin:0px 0px 0px 4px;
49     cursor:pointer;    
50 }
jquery.idcode.css
jquery.idcode.js 脚本:
Jquery 客户端生成验证码Jquery 客户端生成验证码
  1 (function($){
  2     var settings = {
  3             e             : 'idcode',
  4             codeType     : { name : 'follow', len: 4},
  5             codeTip        : 'refresh?',
  6             inputID        : 'Txtidcode'          //引用验证码输入框Id
  7         };
  8     
  9     var _set = {
 10         storeLable  : 'codeval',
 11         store        : '#ehong-code-input',
 12         codeval        : '#ehong-code'
 13     }
 14     $.idcode = {
 15         getCode:function(option){
 16             _commSetting(option);
 17             return _storeData(_set.storeLable, null);
 18         },
 19         setCode:function(option){
 20             _commSetting(option);
 21             _setCodeStyle("#"+settings.e, settings.codeType.name, settings.codeType.len);
 22             
 23         },
 24         validateCode:function(option){
 25             _commSetting(option);
 26             var inputV;
 27             if(settings.inputID){
 28                 inputV=$('#' + settings.inputID).val();
 29             }else{
 30                 inputV=$(_set.store).val();
 31             }
 32             
 33             if(inputV == _storeData(_set.storeLable, null)){
 34                 return true;
 35             }else{
 36                 _setCodeStyle("#"+settings.e, settings.codeType.name, settings.codeType.len);                
 37                 return false;
 38             }
 39         }
 40     };
 41     
 42     function _commSetting(option){
 43         $.extend(settings, option);        
 44     }
 45     
 46     function _storeData(dataLabel, data){
 47         var store = $(_set.codeval).get(0);            
 48         if(data){
 49             $.data(store, dataLabel, data);            
 50         }else{
 51             return $.data(store, dataLabel);            
 52         }
 53     }
 54     
 55     function _setCodeStyle(eid, codeType, codeLength){
 56         var codeObj = _createCode(settings.codeType.name, settings.codeType.len);        
 57         var randNum = Math.floor(Math.random()*6);
 58         var htmlCode=''
 59         if(!settings.inputID){
 60             htmlCode='<span><input id="ehong-code-input" type="text" maxlength="4" /></span>';
 61         }
 62         htmlCode+='<div id="ehong-code" class="ehong-idcode-val ehong-idcode-val';
 63         htmlCode+=String(randNum);
 64         htmlCode+='" href="#" onblur="return false" onfocus="return false" oncontextmenu="return false" onclick="$.idcode.setCode()">' + _setStyle(codeObj) + '</div>' + '<span id="ehong-code-tip-ck" class="ehong-code-val-tip" onclick="$.idcode.setCode()">'/*+ settings.codeTip*/ +'</span>';
 65         $(eid).html(htmlCode);
 66         _storeData(_set.storeLable, codeObj);        
 67     }
 68     
 69     function _setStyle(codeObj){
 70         var fnCodeObj = new Array();
 71         var col = new Array('#BF0C43', '#E69A2A','#707F02','#18975F','#BC3087','#73C841','#780320','#90719B','#1F72D8','#D6A03C','#6B486E','#243F5F','#16BDB5');
 72         var charIndex;
 73            for(var i=0;i<codeObj.length;i++){        
 74             charIndex = Math.floor(Math.random()*col.length);
 75             fnCodeObj.push('<font color="' + col[charIndex] + '">' + codeObj.charAt(i) + '</font>');
 76         }
 77         return fnCodeObj.join('');        
 78     }
 79     function _createCode(codeType, codeLength){
 80        var codeObj;
 81        if(codeType=='follow'){
 82            codeObj = _createCodeFollow(codeLength);
 83        }else if(codeType=='calc'){
 84            codeObj = _createCodeCalc(codeLength);
 85        }else{
 86            codeObj="";
 87        }
 88        return codeObj;   
 89      }
 90      
 91      function _createCodeCalc(codeLength){
 92        var code1, code2, codeResult;
 93        var selectChar = new Array('0','1','2','3','4','5','6','7','8','9');    
 94        var charIndex;
 95        for(var i=0;i<codeLength;i++){        
 96            charIndex = Math.floor(Math.random()*selectChar.length);
 97            code1 +=selectChar[charIndex];
 98            
 99            charIndex = Math.floor(Math.random()*selectChar.length);
100            code2 +=selectChar[charIndex];           
101        }
102        return [parseInt(code1), parseInt(code2) , parseInt(code1) + parseInt(code2)] ;
103      }
104      
105      function _createCodeFollow(codeLength){
106        var code = "";
107        var selectChar = 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');
108         
109        for(var i=0;i<codeLength;i++){        
110            var charIndex = Math.floor(Math.random()*selectChar.length);
111            if(charIndex % 2 == 0){
112                code+=selectChar[charIndex].toLowerCase();
113            }else{
114                code +=selectChar[charIndex];
115            }       
116        }
117        return code;
118      }
119    
120 })(jQuery);
jquery.idcode.js
jquery 的脚本从官网上下载即可。

html代码部分:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<title>jQuery区分大小写验证码</title>

<!--//引用css-->
<link href="css/jquery.idcode.css" type="text/css" rel="stylesheet"> 
</head>
<body>
<center><br><br>
<input type="text" id ="Txtidcode" class ="txtVerification"><span id="idcode"></span>
<input type="button" id="butn" value="提交"></center>
<script src="js/jquery-1.12.3.min.js"></script>
<!--//引用idcode插件-->
<script src="js/jquery.idcode.js"></script>
<script>
    $.idcode.setCode();   //加载生成验证码方法
    $("#butn").click(function(){
        var IsBy = $.idcode.validateCode()  //调用返回值,返回值结果为true或者false
        if(IsBy){
            alert("验证码输入正确")
        }else {
            alert("请重新输入")
        }
    })
</script>
 
</body>
</html>

 

 
 
上一篇:C# 窗体间传值


下一篇:ASP.NET MVC 5 基本构成