thirdalarmpro
asked on
Compare Two CFINPUT fields
Is there a way to compare two CFINPUT fields in a CFFORM tag using Java or Client-Side Validation. I want to compare email_1 to email_2 to ensure the user entered the same email twice. The same goes for the password. Thanks!
John
John
here is a very simple javascript example you can modify......
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form action="validationTest.cfm
email <input type="text" name="email1" id="email1"><br>
retype <input type="text" name="email2" id="email2"><br>
<input type="Submit" value="save" name="action">
</form>
<script language="JavaScript">
function checkFields() {
if (document.myForm.email1.va
alert("Emails do not match. Please correct.");
return false;
}
return true;
}
</script>
</body>
</html>
remmember you have to do server side validation anyway, since the client can turn javascript off.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
rob_lorentz,
The only problem is the user asked with cfform and your solution uses a normal form (onSubmit of a cfform does not work in CF MX 6 and earlier due to it being overwritten by Cold Fusion - I haven't tested CF 7)
The only problem is the user asked with cfform and your solution uses a normal form (onSubmit of a cfform does not work in CF MX 6 and earlier due to it being overwritten by Cold Fusion - I haven't tested CF 7)
my bad, didn't notice the cfform.
move the checkfields function to onClick of then submit button.
<cfform action="validationTest.cfm " method="post" name="myForm" >
email <input type="text" name="email1" id="email1"><br>
retype <input type="text" name="email2" id="email2"><br>
<input type="Submit" value="save" name="action" onclick="javascript:return checkFields();">
</cfform>
move the checkfields function to onClick of then submit button.
<cfform action="validationTest.cfm
email <input type="text" name="email1" id="email1"><br>
retype <input type="text" name="email2" id="email2"><br>
<input type="Submit" value="save" name="action" onclick="javascript:return
</cfform>
that will work if using a regular submit input. :o)
<cfset ErrorMessage = "">
<cfif Form.email_1 NEQ form.email_2>
<cfset ErrorMessage = ListAppend(ErrorMessage, "The email addresses do not match")>
</cfif>
<cfif Form.password_1 NEQ form.password_2>
<cfset ErrorMessage = ListAppend(ErrorMessage, "The passwords do not match")>
</cfif>
<cfif ErrorMessage NEQ 0>
Please correct the following errors: <cfoutput>#ErrorMessage#</
</cfif>