2014-02-13 236 views
0

有人可以幫助我驗證確認密碼和密碼。如何驗證密碼和confrm密碼

現在這是我的代碼:

<script> 

// Form validation 

$(function() { 

// Setup form validation on the #register-form element 
$("#register-form").validate({ 

    // Specify the validation rules 
    rules: { 
     firstname: "required", 
     lastname: "required", 
     gender: "required", 
     email: "required", 
     password: { 
      required: true, 
      minlength: 6 
     }, 
     phone: { 
      required: true, 
      minlength: 11 
     }, 

     agree: "required" 
    }, 

    // Specify the validation error messages 
    messages: { 
     firstname: "Please enter your first name", 
     lastname: "Please enter your last name",   
     password: { 
      required: "Please provide a password", 
      minlength: "Your password must be at least 6 characters long" 
     }, 
     phone: "Please enter a valid phone number", 
     email: "Please enter your email", 
     gender: "Please enter your gender", 
     birthday: "Please enter your birthday", 
     agree: "Please accept our policy" 
    }, 

    submitHandler: function(form) { 
     form.submit(); 
    } 
}); 
}); 
</script> 
+1

你嘗試過什麼?你有哪些麻煩?我看到驗證規則和消息,但他們沒有在任何地方使用 – helion3

回答

0

可以使用equalTo規則

$(function() { 

    // Setup form validation on the #register-form element 
    $("#register-form").validate({ 

     // Specify the validation rules 
     rules: { 
      firstname: "required", 
      lastname: "required", 
      gender: "required", 
      email: "required", 
      password: { 
       required: true, 
       minlength: 6, 
       equalTo: '#confirmPassed'//confirm password selector 
      }, 
      phone: { 
       required: true, 
       minlength: 11 
      }, 

      agree: "required" 
     }, 

     // Specify the validation error messages 
     messages: { 
      firstname: "Please enter your first name", 
      lastname: "Please enter your last name", 
      password: { 
       required: "Please provide a password", 
       minlength: "Your password must be at least 6 characters long", 
       equalTo: 'Confirm Password and Password must match' 
      }, 
      phone: "Please enter a valid phone number", 
      email: "Please enter your email", 
      gender: "Please enter your gender", 
      birthday: "Please enter your birthday", 
      agree: "Please accept our policy" 
     }, 

     submitHandler: function (form) { 
      form.submit(); 
     } 
    }); 
}); 
+0

很好!多謝,夥計! – user3303166