Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Requiring Two Identical Email Addresses

Some forms require a person to type an email address a second time. Software then checks that the email addresses are the same.

The idea is that typing it twice is likely to catch a typographical error, email addresses being critical for communication.

Similar "type it twice" functionality can be applied to passwords or other information that must be correct when submitted.

Here is code is for a working example. Copy it and paste into a test web page to see it working.

<form 
   name="myform" 
   onsubmit="return ErrorCheck();" 
   method="post" 
   action="/cgi-bin/script.cgi"> 

<input 
   type="hidden" 
   name="identicalfields" 
   value="email,emailagain">

<p style="width:300px; text-align:right;">
Email 
<input type="text" name="email" size="22">
</p><p style="width:300px; text-align:right;">
Type again 
<input type="text" name="emailagain" size="22">
</p>
<p style="width:300px; text-align:center;">
<input type="submit">
</p>

</form>

<script type="text/javascript"><!--
function ErrorCheck() {
var formname = "myform";
var fieldwithlist = "identicalfields";
var er = new String();
var tempstring = eval("document."+formname+"."+fieldwithlist+".value");
if(tempstring.length > 1) {
   var idi = tempstring.split(',');
   var ts0 = eval("document."+formname+"."+idi[0]+".value");
   var ts1 = eval("document."+formname+"."+idi[1]+".value");
   if(ts0 != ts1) { er = 'The form field at "'+idi[0]+'" must be identical to the form field at "'+idi[1]+'".'; }
   }
if(er.length > 0) {
   alert(er);
   return false;
   }
return true;
}
//--></script>

The two email fields are name="email" and name="emailagain".

The value of a hidden field name="identicalfields" is a comma-separated list of the field names to compare: value="email,emailagain"

When the submit button is clicked, the JavaScript below the form will compare the two fields. If they are not identical, an alert box with an error message is spawned.

Near the beginning of the JavaScript is are places to specify the name of the form and the name of the hidden field with the comma-separated list. Should those change in your form, the JavaScript will need to know the new values.

Here is a working example, identical to the above except the action= URL in the form tag. Different content in the two fields will cause a JavaScript alert with an error message.

Email

Type again

Note that the above live example will merely re-load this web page when the form is submitted with both fields identical. That is because the example form tag has no action= attribute.

When you need to confirm that two form fields contain identical information, this works a treat.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC