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 ???

LVL 25
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!');

I appreciate your time
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.

<script src="" type="text/javascript"></script>
<script type="text/javascript">
function checkFields(field1, field2){
	if(field2.val() != '' || typeof $fieldsCompleted != 'undefined'){
			alert("Fields match");
			alert("fields dont match");

		checkFields($(this), $('#passwordField2'));
		$fieldsCompleted = 1;
		checkFields($('#passwordField1'), $(this));
		<input type="password" id="passwordField1" />
		<input type="password" id="passwordField2" />

Open in new window

dgrafxAuthor Commented:
I can't try this until Monday but will get back with you then ...
dgrafxAuthor Commented:
The reason is in the post
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.