compare field values with jquery

I'm looking for a "confirm entry" type of function.

Say a user enters a password in a password field then the next field they are asked to confirm password.
IF they do not match then event fires - error msg or ???

Thanks
LVL 25
dgrafxAsked:
Who is Participating?
 
dgrafxAuthor Commented:
I'm really sorry but I couldn't get your code to work at all so I just used this:

if($(f1).val() != '' && $(f2).val() != '' && $(f1).val() != $(f2).val()) {
      alert('These 2 fields must match!');
      $(f2).attr('value','');
}

I appreciate your time
0
 
ChrisCommented:
Just written a quick function that should achieve what you're after. I've put in into an html page with a couple of password fields so you can see it in action - just copy and paste.

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function checkFields(field1, field2){
	if(field2.val() != '' || typeof $fieldsCompleted != 'undefined'){
		if(field1.val()===field2.val()){
			alert("Fields match");
		}
		else{
			alert("fields dont match");
		}
	}
}

$(function(){
	$('#passwordField1').blur(function(){
		checkFields($(this), $('#passwordField2'));
	});
	
	$('#passwordField2').blur(function(){
		$fieldsCompleted = 1;
		checkFields($('#passwordField1'), $(this));
	});
});
</script>
</head>
<body>
	<form> 
		<input type="password" id="passwordField1" />
		<input type="password" id="passwordField2" />
	</form>
</body>

Open in new window

0
 
dgrafxAuthor Commented:
Thanks
I can't try this until Monday but will get back with you then ...
0
 
dgrafxAuthor Commented:
The reason is in the post
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.