Email Address Validation

There will almost certainly be times when you want to request for an email address to be entered on one of your forms. There is no easy way to validate that the email address that is entered actually exists and certainly no way to validate that it belongs to the person that entered it but we can at least validate that the email address is in an acceptable format that is recognisable as an email address.

The validation routine that I am providing here will not reject all invalid email addresses and it will not accept all valid email addresses. Valid addresses that this module will not accept are those where the user name is supplied as a quoted string eg. "my!&#$ email" or which specify an ip address instead of a domain name eg. me@[] but I would not necessarily want email addresses in those formats to be used in my forms even if they are valid. Invalid addresses that would be considered valid are those of the form where the actual username or domain doesn't actually exist.

Here is an example form containing an email address field that uses the validation module:

You are welcome to use this script on any non-commercial web site that you may have. All that I ask is that you don't remove any copyright notices and obey the other comments within the code. If you have a commercial website you may still use the script (subject to the same conditions) in return for a donation toward the cost of my maintaining this site.

After you download and install the email validation module, you need to link it into your page by placing the following code inside the head part of the page:

<script type="text/javascript" src="emailval.js"></script>

You then call it using the following statement:

valid_email = validateEmail(email_field,mandatory,messages);

The email address to be validated needs to be passed as email_field. If the email field is mandatory then the mandatory field should contain '1' otherwise it should be '0'. The messages field can be set to '1' to return individual messages about which particular test that the email address failed to meet. The error messages will be suppressed if this is set to '0' and you can provide your own failure message. The value returned will be true if the email address is considered to be valid and false if it isn't.

If you prefer you can incorporate the call into an if statement processing the appropriate code based directly on the value returned instead of having to test the valid_email field. For example you could place the following into your validation code to validate a mandatory email field:

if (!validateEmail(emailfield,1,0)) {
  alert('email address is invalid or was not entered'); return false;}

The validation routines that I have used in creating this module avoid the use of regular expressions and sticks with the most simple javascript code so the validation module should work with all browsers.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow