Confirm 2 password fields via jQuery

I am trying to find a small script where when entering two password fields and if the passwords do not match then the script will be specified ... I also hope to do this without clicking the Submit button.

Here are my two fields:

<input name="password" type="password" id="password-1" maxlength="30" placeholder="Password Must Be 5-30 Characters" class="text-input required password">
<input name="password" type="password" id="password-2" placeholder="Repeat Password" class="text-input required password">

      

+3


source to share


3 answers


change the names of the input fields,

<input name="password1" type="password" id="password-1" maxlength="30" placeholder="Password Must Be 5-30 Characters" class="text-input required password">
<input name="password2" type="password" id="password-2" placeholder="Repeat Password" class="text-input required password">

      

and use the following script,



$("#password-2").change(function(){
     if($(this).val() != $("#password-1").val()){
               alert("values do not match");
               //more processing here
     }
});

      

If you want to use more features, you should consider using a jquery plugin like jquery Validate

+4


source


You can do it like:

$('.password').change(function(){
      if($("#password-1").val() == $("#password-2").val()){
            /* they match */
      }else{
            /* they are different */
      }
 });

      

this will bind to the class you put on both of these fields, and whenever someone changes the field or , they will do this check.

you can also use $('input[type=password]')

if you didn't have these classes ...



here's some additional readings:

http://www.designchemical.com/blog/index.php/jquery/check-passwords-using-jquery/

http://bmharwani.com/blog/2010/11/12/matching-the-password-and-confirm-password-fields-in-jquery/

+3


source


you can get this out of the box with the jQuery validation plugin . There are many documents and guides for you, including to ensure that the two fields match.

0


source







All Articles