Registration process on web sites requires entering password and password confirmation fields. Here is an example how to validate password and confirm password fields with jQuery.
HTML code with form to be validated
<form>
<div>
<span class="label">User Name</span>
<input type="text" class="userid" name="userid"/>
</div>
<div>
<span class="label">Password</span>
<input type="password" class="password" name="password" />
</div>
<div>
<span class="label">Confirm Password</span>
<input type="password" class="confpass"name="confpass" />
</div>
<input id="submit" type="submit" value="Submit">
</form>
jQuery code for checking values entered into password and confirm password fields:
$(document).ready(function() {
$('#submit').click(function(event){
data = $('.password').val();
var len = data.length;
if(len < 1) {
alert("Password cannot be blank");
// Prevent form submission
event.preventDefault();
}
if($('.password').val() != $('.confpass').val()) {
alert("Password and Confirm Password don't match");
// Prevent form submission
event.preventDefault();
}
});
});