首页 > 代码库 > js正则表达式之密码强度验证

js正则表达式之密码强度验证

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>passwordCheck</title>  
<script type="text/javascript" src="http://www.mamicode.com/Js/jquery1.6.1.js"></script>  
<script type="text/javascript">  
    $(function() {  
        $(‘#pwd‘).keyup(function() {  
            var val = $(this).val();  
            isDigit(val);  
        });  
          
        function isDigit(s) {  
            var pattern_d = /^\d+$/;            //全数字  
            var pattern_s = /^[A-Za-z]+$/       //全字符  
            var pattern_w = /^\w+$/;            //数字或者字符  
            var pattern_W = /^\W+$/             //全非数字也非字符  
            var pattern_r = /^\w+\W+[\w\W]*\w+$/    //以字母或者数字开头结尾的字符串  
            var html = ‘‘;  
            var x = 0;  
            var y = 0;  
              
            $(‘#user‘).html(s);  
            if(pattern_W.exec(s)) {  
                html += ‘非数字也非字符<br />‘;  
                x = 0;  
                y = 0;  
            }  
            if(pattern_w.exec(s)) {  
                html += ‘数字或者字符<br />‘;  
                y = 1;  
            }  
            if(pattern_d.exec(s)) {  
                html += ‘全数字<br />‘;  
                x = 1;  
                y = 0;  
            }  
            if(pattern_s.exec(s)) {  
                html += ‘全字符<br />‘;  
                x = 2;  
                y = 0;  
            }  
            if(pattern_r.exec(s)) {  
                html += ‘以字母或者数字开头结尾的字符串<br />‘;  
                x = 3;  
                y = 2;  
            }  
            if( y === 0 && x === 0) {  
                html += ‘密码格式错误<br />‘;  
            }  
            if( x > 0 && y === 0) {  
                html += ‘安全级别《低》<br />‘;  
            }  
            if( x === 0 && y === 1) {  
                html += ‘安全级别《中》<br />‘;  
            }  
            if( y === 2) {  
                html += ‘安全级别《高》<br />‘;  
            }  
            html += x + ‘<br />‘ + y;  
            $(‘#password‘).html(html);  
        };  
    });  
</script>  
</head>  
  
<body>  
<form action="#" method="#">  
    用户名:<input type="text" name="" /><span id="user"></span><br />  
    密码:<input type="password" name="" id="pwd" /><span id="password"></span><br />  
</form>  
</body>  
</html>  

  

js正则表达式之密码强度验证