Created August 1999; Updated October 2002

Compare two fields, Submit only if they are the same:

Question:

How do I setup a form to validate a user's email address by having the user type in his/her address twice, then having the form compare the fields to see if the two entries match?

Demonstration:

Enter Your Email Address:

Please Confirm Your Email Address:

Source Code: JavaScript and HTML

<script type="text/javascript" language="JavaScript">
<!--
//--------------------------------
// This code compares two fields in a form and submit it
// if they're the same, or not if they're different.
//--------------------------------
function checkEmail(theForm) {
    if (theForm.EMAIL_1.value != theForm.EMAIL_2.value)
    {
        alert('Those emails don\'t match!');
        return false;
    } else {
        return true;
    }
}
//-->
</script> 




<form action="../" onsubmit="return checkEmail(this);">
<p> Enter Your Email Address:<br>
<input type="TEXT" name="EMAIL_1" size="20" maxlength="20"> 
<br>
Please Confirm Your Email Address:
<br>
<input type="TEXT" name="EMAIL_2" size="20" maxlength="20"> 
<br>
<input type="SUBMIT" value="Send Address!"></p> 
</form>

Source Code

<script type="text/javascript" language="JavaScript">
<!--
//--------------------------------
// This code compares two fields in a form and submit it
// if they're the same, or not if they're different.
//--------------------------------
function checkEmail(theForm) {
	if (theForm.EMAIL_1.value != theForm.EMAIL_2.value)
	{
		alert('Those emails don\'t match!');
		return false;
	} else {
		return true;
	}
}
//-->
</script>