Skip to content

Instantly share code, notes, and snippets.

@grayghostvisuals
Created October 15, 2013 00:12
Show Gist options
  • Save grayghostvisuals/6984561 to your computer and use it in GitHub Desktop.
Save grayghostvisuals/6984561 to your computer and use it in GitHub Desktop.
A simplistic way to make sure two password fields match using jQueryValidation (http://jqueryvalidation.org/validate) Using minlength, and a custom message.
jQuery.validator.addMethod( 'passwordMatch', function(value, element) {
// The two password inputs
var password = $("#register-password").val();
var confirmPassword = $("#register-pass-confirm").val();
// Check for equality with the password inputs
if (password != confirmPassword ) {
return false;
} else {
return true;
}
}, "Your Passwords Must Match");
// ==========================================================================
// Registration Form : jquery validation
// http://jqueryvalidation.org/validate
$('#form-register').validate({
// rules
rules: {
register_password: {
required: true,
minlength: 3
},
register_pass_confirm: {
required: true,
minlength: 3,
passwordMatch: true // set this on the field you're trying to match
}
},
// messages
messages: {
register_password: {
required: "What is your password?",
minlength: "Your password must contain more than 3 characters"
},
register_pass_confirm: {
required: "You must confirm your password",
minlength: "Your password must contain more than 3 characters",
passwordMatch: "Your Passwords Must Match" // custom message for mismatched passwords
}
}
});//end validate
@rwwaskk
Copy link

rwwaskk commented Sep 5, 2014

Works great. Thanks 👍

@kpatodia
Copy link

This works great - i have one issue though , i don't want it to fire onblur , i tried adding onblur:false, onfocusout:false,onkeyup:false - but none of them worked. On tab out , it is making the text box red - but not showing the error message. On Submit it works just fine.

@chamithkanchana
Copy link

Great.. This works. Thank you..

@rabnawazPikesSoft
Copy link

appericated

@asimshazad
Copy link

asimshazad commented Aug 10, 2016

Appreciated

@Puwaga
Copy link

Puwaga commented Aug 29, 2017

thanks

@oluwaseunladeinde
Copy link

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment