2017-08-08 83 views
0

我使用jquery.validate,我想在用戶輸入時驗證密碼,但它不起作用。jquery.validate pwcheck密碼不起作用

檢查pwcheck密碼不起作用。請看我有什麼問題嗎?

if (jQuery().validate) { 
    $.validator.addMethod("pwcheck", function(value) { 
    return /^[A-Za-z0-9\d=!\[email protected]_*]*$/.test(value) // consists of only these 
     && 
     /[a-z]/.test(value) // has a lowercase letter 
     && 
     /\d/.test(value) // has a digit 
    }); 

    $('#myform').validate({ 
    rules: { 
     forename: { 
     required: true, 
     maxlength: 300 
     }, 
     surname: { 
     required: true, 
     maxlength: 300 
     }, 
     password: { 
     required: true, 
     minlength: 8, 
     maxlength: 300 
     }, 
     password_repeat: { 
     required: true, 
     minlength: 8, 
     maxlength: 300, 
     equalTo: '#password' 
     } 
    }, 
    messages: { 
     password: { 
     pwcheck: "Das Passwort entspricht nicht den Kriterien!", 
     } 
    }, 
    submitHandler: function(form) { 
     if (grecaptcha.getResponse()) { 
     form.submit(); 
     } else { 
     alert('Please complete the reCaptcha to proceed'); 
     } 
    } 
    }); 
}); 
+1

您創建的名爲'pwcheck'新規則,但你不要在任何地方使用它。 – Sparky

+0

我解決了我的問題。原因,因爲我沒有添加'pwcheck:true' –

回答

0

你應該使用這樣的:

在頂部而不是底部添加equalTo: "#password",

password: { 
    required: true, 
    pwcheck: true, 
    minlength: 8, 
    maxlength: 300 
}, 
password_repeat: { 
    equalTo: "#password", 
    required: true, 
    minlength: 8, 
    maxlength: 300, 
} 

添加在這裏:

/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/ 

然而,這是很容易做到這一點沒有前瞻:

$.validator.addMethod("pwcheck", function(value) { 
    return /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/.test(value) // consists of only these 
     && /[a-z]/.test(value) // has a lowercase letter 
     && /\d/.test(value) // has a digit 
}); 
+0

我想輸入時要驗證密碼,用戶必須輸入包含小寫,大寫字母,特殊字符,長度> 8 –

+0

我用pwcheck檢查但沒有工作 –

+1

檢查更新代碼在密碼中使用'pwcheck:true,'.. –