?
Solved

Compare Two CFINPUT fields

Posted on 2005-04-14
6
Medium Priority
?
603 Views
Last Modified: 2013-12-24
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
0
Comment
Question by:thirdalarmpro
  • 4
  • 2
6 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 13785379
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
 
LVL 10

Expert Comment

by:rob_lorentz
ID: 13785382


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

Accepted Solution

by:
mrichmon earned 2000 total points
ID: 13785402
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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 35

Expert Comment

by:mrichmon
ID: 13785413
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
 
LVL 10

Expert Comment

by:rob_lorentz
ID: 13785574
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
 
LVL 35

Expert Comment

by:mrichmon
ID: 13785609
that will work if using a regular submit input. :o)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

809 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