Link to home
Start Free TrialLog in
Avatar of thirdalarmpro
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
Avatar of mrichmon
mrichmon

Yes. But you want to do the comparison serverside.  Client side is nice as an added benefit, but people can turn off javascript so you need to make sure to compare with serverside

<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#</cfoutput>
</cfif>


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" method="post" name="myForm" onSubmit="javascript:return checkFields();">

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.value != document.myForm.email2.value) {
            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
Avatar of mrichmon
mrichmon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
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>
that will work if using a regular submit input. :o)