compare field values with jquery

Posted on 2012-08-24
Last Modified: 2012-09-01
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 ???

Question by:dgrafx
    LVL 12

    Expert Comment

    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

    LVL 24

    Author Comment

    I can't try this until Monday but will get back with you then ...
    LVL 24

    Accepted Solution

    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
    LVL 24

    Author Closing Comment

    The reason is in the post

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question ( on how to make a page show some balloons animate up a page…
    There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now