qq78707 发表于 2017-4-12 09:22:44

php判断密码强度实现和JS密码强度验证

功能:判断密码强度

结果形式:

打分(满分为10)
  代码:

           $score = 0;

           if(preg_match("/+/",$str))

           {

              $score ++; 

           }

           if(preg_match("/{3,}/",$str))

           {

              $score ++; 

           }

           if(preg_match("/+/",$str))

           {

              $score ++; 

           }

           if(preg_match("/{3,}/",$str))

           {

              $score ++; 

           }

           if(preg_match("/+/",$str))

           {

              $score ++; 

           }

           if(preg_match("/{3,}/",$str))

           {

              $score ++; 

           }

           if(preg_match("/+/",$str))

           {

              $score += 2; 

           }

           if(preg_match("/{3,}/",$str))

           {

              $score ++ ; 

           }

           if(strlen($str) >= 10)

           {

              $score ++; 

           }

           echo "<br>";

           echo $score;

 

JS实现密码强度验证:

<script type="text/javascript">
var $ = function(v){return document.getElementById(v);}
function isSecurity(v){
if (v.length < 3) { iss.reset();return;}
var lv = -1;
if (v.match(//ig)){lv++;}
if (v.match(//ig)){lv++;}
if (v.match(/(.[^a-z0-9])/ig)){lv++;}
if (v.length < 6 && lv > 0){lv--;}
iss.reset();
switch(lv) {
case 0:
iss.level0();
break;
case 1:
iss.level1();
break;
case 2:
iss.level2();
break;
default:
iss.reset();
}
}
var iss = {
color:["CC0000","FFCC33","66CC00","CCCCCC"],
text:["弱","中","强"],
width:["50","100","150","10"],
reset:function(){
$("B").style.backgroundColor = iss.color;
$("B").style.width = iss.width;
$("A").innerHTML = "验证试中";
},
level0:function(){
$("B").style.backgroundColor = iss.color;
$("B").style.width = iss.width;
$("A").innerHTML = "较弱";
},
level1:function(){
$("B").style.backgroundColor = iss.color;
$("B").style.width = iss.width;
$("A").innerHTML = "中";
},
level2:function(){
$("B").style.backgroundColor = iss.color;
$("B").style.width = iss.width;
$("A").innerHTML = "高强";
}
}
</script>
<input type="password" name="password" size="25" maxlength="20" onkeyup="isSecurity(this.value);">
<font class="red" id="A">密码强度</font>
<table height="8" border="1" align="left" cellpadding="0" cellspacing="0" bordercolor="#EEEEEE" style="border-collapse:collapse;">
<tr>
<td bgcolor="#EEEEEE" width="1" align="center" valign="middle" id="B"></td>
</tr>
</table>
 
页: [1]
查看完整版本: php判断密码强度实现和JS密码强度验证