开发者

jquery confirm password validation

I am using jquery for form validation. Rest is well except the confirm password field. Even when the same password is typed, the Please enter the same password. is not removed.

My script is:

  <script type="text/javascript">
    $(document).ready(function() {
      $("#form1").validate({
        rules: {
          password: { 
                required: true, minlength: 5
          }, 
          c_password: { 
                required: true, equalTo: "#password", minlength: 5
          }, 
        email: {
          required: true, email: true
          },
        phone: {
          required: true, number: true, minlength: 7
          },
        url: {
          url: true
        },
        description: {
          required: true
        },
        gender: {
             required: true
          }
        },
        messages: {
         description: "Please enter a short description.",
  开发者_开发技巧       gender: "Please select your gender."

        }
      });
    });
     -->
  </script>

And inside the form tag:

<div class="form-row"><span class="label">Password</span><input type="password" name="password" class="required" id="password" /></div>

<div class="form-row"><span class="label">Confirm Password</span><input type="password" name="c_password" id="c_password" /></div>

Any suggestion? Would be much thankful for the help.


Your fields doesn't have the ID property. In jQuery the "#password" selector means "the object that has an id property with value 'password'"

Your code should look like this:

<input type="password" name="password" id="password" class="required"/>


<input type="password" id="password" class="nomal required error" value="" name="cpassword">

<input type="password" equalto="#password" class="nomal required error" value="" name="cpassword">


Be sure that your password's input text has id 'password'


rules: {

isn't closed. Put another } after:

messages: {
   description: "Please enter a short description.",
   gender: "Please select yor gender."
}

This is as well as the answer about the element ID's.


Be sure that no other input with tag id='password' in that same page.


I came across some issues when using camelCase id's for the password inputs. I finnally got it working with different 'name' and 'id'.

<input type="password" id="pass" placeholder="New password" name="newPassword">
<input type="password" id="pass2" placeholder="New password" name="repeatNewPassword">
<input type="email" id="inputNewEmail" name="newEmail" placeholder="New email">
<input type="email" id="repeatNewEmail" name="repeatNewEmail" placeholder="New email>

Then in the JS:

rules: {
            newPassword: {
                minlength: 6
            },
            repeatNewPassword: {
                minlength: 6,
                equalTo: "#pass"
            },
            newEmail: { email: true},
            repeatNewEmail: {email: true, equalTo: '#inputNewEmail'},
}

So refer to the input field by its 'name' but define equalTo deppendency using 'id'. I'm not sure about the camelCase issue, for the email inputs it worked, but exactly same nomenclature with password inputs didn't work, according to my experience


if($("#newpassword").val()!== ($("#conformpassword").val())){
    $('#newpasswordId').html('<font color="red">Your password does not match</font>');
    $("#newpassword").val('');
    $("#conformpassword").val('');
    $('#newpassword').css("border", "#FF0000 solid 1px")
    $('#conformpassword').css("border", "#FF0000 solid 1px")
    $("#newpassword").focus();

    return false;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜