Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

confirm field values

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
US
Hi all, I have a form where I have two sets of fields that need to the same value. I am using this code and that works, but I'd like to know if there is an easier way.

Code:
function confirmIt () {
	var confirmHRE = document.registrationForm;
	var errorMSG = "";
	
	if(confirmHRE.Password.value != confirmHRE.ConfirmPassword.value) { 
		alert("Password does not match.  Please retry.");
		return false;
	}
	
	if(confirmHRE.Email.value != confirmHRE.ConfirmEmail.value) { 
		alert("Email does not match.  Please retry.");
		return false;
	}
	
}

See how I have to retype the if statement again for the secnond field? Is there an easier way?

____________________________________
Just Imagine.
 
what?? I'm sorry but you'll have to spend 10 extra seconds of your life retyping that field. :p
 
There are easier ways, but for two fields, it's not really worth the rework, as the code might possibly end up longer (especially given that the error messages are different).

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks guys. I was just curious cause what if the two fields turn into ten fields, or more. I don't have a problem doing it the way I am doing now. I mean, i'd just be copy/pasting, lol.



____________________________________
Just Imagine.
 
BillyRayPreachersSon, well, if I were to get an easier way then i'd make the error message something generic.

____________________________________
Just Imagine.
 
gUjU, here's a way (untested):

Code:
function confirmIt () {
    var confirmHRE = document.registrationForm;
    var e = confirmHRE.elements;
    var a = new Array("Password", "Email", "OtherField");

    for ( var i = 0; i < a.length; i++ ) {
        if ( e[a[i]].value != e["Confirm" + a[i]].value ) {
            alert("Your " + a[i] + " fields do not match!");
            return false;
        }
    }
}



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top