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
thirdalarmproAsked:
Who is Participating?
 
mrichmonConnect With a Mentor Commented:
Javascript for clientside:

<script type="text/javascript">
function Validate()
{
      if(document.formname.email_1.value != document.formname.email_2.value)
               alert("The email addresses do not match");
      if(document.formname.password_1.value != document.formname.password_2.value)
               alert("The passwords do not match");
}

Then call whenever you want in the form such as the onBlur of the second password and second email fields i.e. onBlur="Validate();"
0
 
mrichmonCommented:
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>
0
 
rob_lorentzCommented:


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.

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
mrichmonCommented:
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)
0
 
rob_lorentzCommented:
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>
0
 
mrichmonCommented:
that will work if using a regular submit input. :o)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.