2017-06-14 88 views
0

我正在使用jquery驗證來檢查密碼強度,但它不起作用。確認密碼匹配部分正在工作,但不是自定義規則。請問我在這裏錯過了什麼?密碼強度驗證

腳本:

HTML:

<label class="typ2">Old Password:</label> 
     @Html.Password("Password", null, new { @class = "typ2 shad1 trans1", maxlength = "50", required = "required" }) 
     <br class="floatClear" /> 

     <label class="typ2">Login Password:</label> 
     @Html.Password("NewPassword", null, new { @class = "typ2 shad1 trans1", maxlength = "50", id = "password", required = "required" }) 
     <span style="position: absolute;margin-top: 0;margin-left: 10px;">Please ensure your password contains an uppercase letter, number and symbol.<br /> Passwords must be between 8 &amp; 15 characters.</span> 
     <label style="color:red !important; margin-left: 10px; font-size:24px;">*</label> 
     <br class="floatClear" /> 

     <label class="typ2">Confirm Password:</label> 
     @Html.Password("ConfirmPassword", null, new { @class = "typ2 shad1 trans1", minlength = "8", maxlength = "50", id = "confirm", required = "required" }) 
     <br class="floatClear" /> 
+0

你是否試圖通過限制字符庫和限制最大長度來強制執行強密碼? –

+0

相關xkcd:[密碼強度](https://xkcd.com/936/) – Andreas

+0

lol Andreas,是的,我不希望如此限制,但這是客戶想要的。由於錯誤消息表明密碼必須包含大寫字母和數字,並且介於8到15個字符之間。 – BMills

回答

1

報價OP的comment

我終於想通了這個問題。我需要將'pwcheck'添加到輸入的類名稱。對類似問題的其他答案都沒有表明這一關鍵步驟。 :(

領域的類中把pwcheck不是一個「關鍵的一步」,這只是一個分配規則,以不同的方式有幾種方法使用這個插件來分配規則

你的根本問題是。下面,當通過rules對象分配規則,你必須使用相應的字段的name由於pwRules是不是現場的name,這個規則分配被忽略。

$('#changePassword').validate({ 
    rules: { 
     ConfirmPassword: {  // <- field NAME 
      equalTo: "#password" // <- rule 
     }, 
     pwRules: { // <- this is not the NAME of a field 
      pwcheck :true 
     } 
    .... 

應該有點像...

rules: { 
     ConfirmPassword: {  // <- field NAME 
      equalTo: "#password" // <- rule 
     }, 
     Password: {    // <- field NAME 
      pwcheck :true   // <- rule 
     } ....