2017-03-01 63 views
0

我有一個簡單的確認密碼jquery腳本,它根本不起作用... 表單被驗證每個長度的每個密碼。不能使用jquery確認密碼驗證

這是形式

<form action="${updatePassword}" method="post" class="form-signin" id="form-signin"> 

    <div class="form-group"> 
     <input type="password" id="password" name="password" placeholder="Enter new Password..." class="form-password form-control"> 
    </div> 
    <div class="form-group"> 
     <input type="password" name="confirmpassword" placeholder="Confirm Password..." class="form-password form-control" id="confirmpassword"> 
    </div> 
    <button type="submit" class="btn" id="submit" name="submit> Reset password</button> 
</form> 

,這是腳本:

$(function() { 
    $("#submit").click(function() //input button click 
    { 
     $.validator.setDefaults({ 
      debug: true, 
      success: "valid" 
     }); 

     $("#form-signin").validate({ 
      rules: { 
       password: { 
        required: true, 
        minlength: 5 

       }, 

       confirmpassword: { 
        equalTo: "#password" 
       } 

      }, 
      messages: { 
       password: { 
        required: "Inserire una password", 
        minlength: "La password deve contenere almeno 5 caratteri" 
       }, 
       confirmpassword: { 
        equalTo: "Le due password devono coincidere" 
       } 
      } 

     }); 
    }); 
}); 

它看起來相當線性...

回答

1

當按鈕類型爲提交則不需要檢查點擊事件。如果您使用過jQuery驗證插件,表單將自動驗證。

並修改像貝爾柳代碼:

    $("#form-signin").validate({ 
       rules: { 
        password: { 
        required: true, 
        minlength: 5 

        } , 

         confirmpassword: { 
         equalTo: "#password" 
        } 

       }, 
       messages: { 
        password: { 
         required: "Inserire una password", 
         minlength: "La password deve contenere almeno 5 caratteri" 
        }, 
        confirmpassword: { 
         equalTo: "Le due password devono coincidere" 
        } 
       } 

      }); 

這裏是Working Code

+0

試過......但沒有任何改變......看這裏https://jsfiddle.net/u82Lorp4/ – besmart

+1

@besmart喜,我已經更新了代碼,現在正在工作。請檢查一下。 https://jsfiddle.net/u82Lorp4/1/其實你還沒有包含jQuery驗證插件。 –

2

它看起來並不像你需要重視點擊處理程序。

拆除包裝$("#submit").click(function() {

所以,你的代碼看起來像:

$(function() { 
    $.validator.setDefaults({ 
     debug: true, 
     success: "valid" 
    }); 

    $("#form-signin").validate({ 
     // ... 
    }); 
});