jquery实例
代码如下 | 复制代码 |
<script> function AuthPasswd(string) { if(string.length >=6) { if(/[a-zA-Z]+/.test(string) && /[0-9]+/.test(string) && /W+D+/.test(string)) { noticeAssign(1); }else if(/[a-zA-Z]+/.test(string) || /[0-9]+/.test(string) || /W+D+/.test(string)) { if(/[a-zA-Z]+/.test(string) && /[0-9]+/.test(string)) { noticeAssign(-1); }else if(/[a-zA-Z]+/.test(string) && /W+D+/.test(string)) { noticeAssign(-1); }else if(/[0-9]+/.test(string) && /W+D+/.test(string)) { noticeAssign(-1); }else{ noticeAssign(0); } } }else{ noticeAssign(null); } } function noticeAssign(num) { |
javascript 例2
代码如下 | 复制代码 |
function testpass(password,username) { var score = 0; if (password.length < 4 ) { return -4; } if (typeof(username) != 'undefined' && password.toLowerCase() == username.toLowerCase()){return -2} score += password.length * 4; score += ( repeat(1,password).length - password.length ) * 1; score += ( repeat(2,password).length - password.length ) * 1; score += ( repeat(3,password).length - password.length ) * 1; score += ( repeat(4,password).length - password.length ) * 1; if (password.match(/(.*[0-9].*[0-9].*[0-9])/)){ score += 5;} if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){ score += 5 ;} if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){ score += 10;} if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)){ score += 15;} if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)){ score += 15;} if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)){score += 15;} if (password.match(/^w+$/) || password.match(/^d+$/) ){ score -= 10;} if ( score < 0 ){score = 0;} if ( score > 100 ){ score = 100;} return score; function repeat(len,str) { var res = ""; for (var i = 0; i < str.length; i++ ) { var repeated = true; for (var j = 0, max = str.length - i - len; j < len && j < max; j++) { repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + len)); } if (j < len) repeated = false; if (repeated) { i += len - 1; repeated = false; } else { res += str.charAt(i); } } return res; } } |
通过上述函数可对密码进行评分验证。
举例:
代码如下 | 复制代码 |
function checkpass(pass) { var username = document.getElementById('username').value; var score = testpass(pass.value,username); var password_label = document.getElementById('password_label'); if(score == -4) { password_label.innerHTML = '太短'; } else if(score == -2) { password_label.innerHTML = '与用户名相同'; } else { var color = score < 34 ? '#edabab' : (score < 68 ? '#ede3ab' : '#d3edab'); var text = score < 34 ? '弱' : (score < 68 ? '一般' : '很好'); var width = score + '%'; password_label.innerHTML = "<span style='width:"+width+";display:block;overflow:hidden;height:20px;line-height:20px;background:"+color+";'>"+text+"</span>"; } } </script> 请输入用户名:<br> <input type="text" class="inpt" name="username" style="width:160px" id="username" /><br> 请输入密码:<br> <input type="password" class="inpt" style="width:160px" onkeyup="javascript:checkpass(this)" name="pass" id="pass" /><br> <span id="password_label" style="width:160px;border:1px solid #F0F0F0"></span> |
例3
代码如下 | 复制代码 |
<script language=javascript> //CharMode函数 //测试某个字符是属于哪一类. function CharMode(iN){ if (iN>=48 && iN <=57) //数字 return 1; if (iN>=65 && iN <=90) //大写字母 return 2; if (iN>=97 && iN <=122) //小写 return 4; else return 8; //特殊字符 } //bitTotal函数 //计算出当前密码当中一共有多少种模式 function bitTotal(num){ modes=0; for (i=0;i<4;i++){ if (num & 1) modes++; num>>>=1; } return modes; } //checkStrong函数 //返回密码的强度级别 function checkStrong(sPW){ if (sPW.length<=4) return 0; //密码太短 Modes=0; for (i=0;i<sPW.length;i++){ //测试每一个字符的类别并统计一共有多少种模式. Modes|=CharMode(sPW.charCodeAt(i)); } return bitTotal(Modes); } //pwStrength函数 //当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色 function pwStrength(pwd){ O_color="#eeeeee"; L_color="#FF0000"; M_color="#FF9900"; H_color="#33CC00"; if (pwd==null||pwd==''){ Lcolor=Mcolor=Hcolor=O_color; } else{ S_level=checkStrong(pwd); switch(S_level) { case 0: Lcolor=Mcolor=Hcolor=O_color; case 1: Lcolor=L_color; Mcolor=Hcolor=O_color; break; case 2: Lcolor=Mcolor=M_color; Hcolor=O_color; break; default: Lcolor=Mcolor=Hcolor=H_color; } } document.getElementById("strength_L").style.background=Lcolor; document.getElementById("strength_M").style.background=Mcolor; document.getElementById("strength_H").style.background=Hcolor; return; } </script> <form name=form1 action="" > 输入密码: <input type=password size=10 onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value)> <br>密码强度: <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc" height="23" style='display:inline'> <tr align="center" bgcolor="#eeeeee"> <td width="33%" id="strength_L">弱</td> <td width="33%" id="strength_M">中</td> <td width="33%" id="strength_H">强</td> </tr> </table> </form> |