在密码字段验证的keyup事件应该执行

时间:2015-06-07 04:49:56

标签: javascript jquery validation

使用jquery进行表单验证 Onkeyup事件密码输入字段被验证为最少8个字符的一个小写字母,一个大写字母,一个数字和一个特殊字符

2 个答案:

答案 0 :(得分:0)



function validatePassword() {
    var password = $("#password").val();
    

    if (password.match(/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@%!])[0-9a-zA-Z@%!]{8,}$/))
        $("#divPasswordValidationResult").html("pass");
    else
         $("#divPasswordValidationResult").html("fail");
   
}

$(document).ready(function () {
   $("#password").keyup(validatePassword);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="td">
    <input type="password" id="password" />
</div>

    <div class="registrationFormAlert" id="divPasswordValidationResult">
</div>
&#13;
&#13;
&#13;

正则表达式的解释是:

(/^
(?=.*\d)                //should contain at least one digit  
(?=.*[a-z])             //should contain at least one lower case  
(?=.*[A-Z])             //should contain at least one upper case 
(?=.*[@%!])             //should contain at least one of @,%,! (you can add more if needed) 
[0-9a-zA-Z@%!]{8,}         //should contain at least 8 from the mentioned characters  
$/)  

答案 1 :(得分:0)

一个例子:(取你喜欢的作品/适合你的代码)

function testPassword(pw) {
    var len = pw.length;
    if(len < 8) return "Too small";
    if(pw.replace(/[a-z]/,'').length > len - 1) return "Need Lowercase Letter";
    if(pw.replace(/[A-Z]/,'').length > len - 1) return "Need Uppercase Letter";
    if(pw.replace(/[0-9]/,'').length > len - 1) return "Need number";
    if(pw.replace(/[!@#$%^&*-]/,'').length > len - 1) return "Need symbol";
    return "GOOD";
}
pw="tessAdfsfdsa9s";
result=testPassword(pw);
if(result=="GOOD"){
    WHATEVER
}
else{
    alert(result);
}