1 <div class="form-group">
2 <i class="icons icon-pwd2"></i>
3 <input type="text" class="form-control" name="newpassword" placeholder="请输入新密码" id="newpassword" />
4 </div>
1 <div class="form-group" id="hide" style="display: none;">
2 <tr>
3 <td>密码强度:</td>
4 <td id="idSM1" align="middle" width="20%"><span style="height:0px; line-height:0px;"> </span><span id="idSMT1" style="DISPLAY: none">弱</span></td>
5 <td id="idSM2" style="BORDER-LEFT: #fff 1px solid" align="middle" width="20%"><span style="height:0px; line-height:0px;"> </span><span id="idSMT0" style="DISPLAY:inline; FONT-WEIGHT: normal; COLOR: #666">无</span><span id="idSMT2" style="DISPLAY: none; color: #FFBB00;">中等</span></td>
6 <td id="idSM3" style="BORDER-LEFT: #fff 1px solid" align="middle" width="20%"><span style="height:0px; line-height:0px;"> </span><span id="idSMT3" style="DISPLAY: none; color:#E63F00;">强</span></td>
7 <td id="idSM4" style="BORDER-LEFT: #fff 1px solid" align="middle" width="20%"> <span style="height:0px; line-height:0px;"> </span><span id="idSMT4" style="DISPLAY: none; color: #FF4500;">极好</span></td>
8 </tr>
9</div>
1 $(function(){
2 // 判断密码强弱级别
3 $(document).on('blur','#newpassword',function(){
4 $('#hide').show();
5 var value = $("input[name='newpassword']").val().trim();
6 if(value == ''){
7 $('#hide').hide();
8 $('#idSMT1').hide();
9 $('#idSMT0').hide();
10 $('#idSMT2').hide();
11 $('#idSMT3').hide();
12 $('#idSMT4').hide();
13 }
14
15 $.post('judgepassword',{value:value},function(data){
16 if(data>=1 && data<=3){
17 $('#idSM1').attr('class','pwdChkCon1'); //弱
18 $('#idSM2').attr('class','pwdChkCon0');
19 $('#idSM3').attr('class','pwdChkCon0');
20 $('#idSM4').attr('class','pwdChkCon0');
21 $('#idSMT1').show();
22 $('#idSMT0').hide();
23 $('#idSMT2').hide();
24 $('#idSMT3').hide();
25 $('#idSMT4').hide();
26 } else if(data>=4 && data<=6){ //中等
27 $('#idSM1').attr('class','pwdChkCon2');
28 $('#idSM2').attr('class','pwdChkCon2');
29 $('#idSM3').attr('class','pwdChkCon0');
30 $('#idSM4').attr('class','pwdChkCon0');
31 $('#idSMT0').hide();
32 $('#idSMT1').hide();
33 $('#idSMT2').show();
34 $('#idSMT3').hide();
35 $('#idSMT4').hide();
36 } else if(data>=7 && data<=8){ //强
37 $('#idSM1').attr('class','pwdChkCon3');
38 $('#idSM2').attr('class','pwdChkCon3');
39 $('#idSM3').attr('class','pwdChkCon3');
40 $('#idSM4').attr('class','pwdChkCon0');
41 $('#idSMT0').hide();
42 $('#idSMT1').hide();
43 $('#idSMT2').hide();
44 $('#idSMT3').show();
45 $('#idSMT4').hide();
46 } else if(data>=9 && data<=10){ //极好
47 $('#idSM1').attr('class','pwdChkCon4');
48 $('#idSM2').attr('class','pwdChkCon4');
49 $('#idSM3').attr('class','pwdChkCon4');
50 $('#idSM4').attr('class','pwdChkCon4');
51 $('#idSMT0').hide();
52 $('#idSMT1').hide();
53 $('#idSMT2').hide();
54 $('#idSMT3').hide();
55 $('#idSMT4').show();
56 }
57 });
58 });
59 });
PHP代码:
1 /**
2 * 判断密码重点级别
3 * @return [type] [description]
4 */
5 public function judgepassword()
6 {
7 $score = 0;
8 if(!empty($_POST['value'])){ //接收的值
9 $str = $_POST['value'];
10 } else{
11 $str = '';
12 }
13 if(preg_match("/[0-9]+/",$str))
14 {
15 $score ++;
16 }
17 if(preg_match("/[0-9]{3,}/",$str))
18 {
19 $score ++;
20 }
21 if(preg_match("/[a-z]+/",$str))
22 {
23 $score ++;
24 }
25 if(preg_match("/[a-z]{3,}/",$str))
26 {
27 $score ++;
28 }
29 if(preg_match("/[A-Z]+/",$str))
30 {
31 $score ++;
32 }
33 if(preg_match("/[A-Z]{3,}/",$str))
34 {
35 $score ++;
36 }
37 if(preg_match("/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)]+/",$str))
38 {
39 $score += 2;
40 }
41 if(preg_match("/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)]{3,}/",$str))
42 {
43 $score ++ ;
44 }
45 if(strlen($str) >= 10)
46 {
47 $score ++;
48 }
49 echo $score;
50 }
![](https://images2018.cnblogs.com/blog/1251731/201806/1251731-20180611105821389-2090045624.png)