Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

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
0
dgrafx
Asked:
dgrafx
  • 3
1 Solution
 
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:
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
 
dgrafxAuthor Commented:
The reason is in the post
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now