Note these scripts do not verify if the eMail address exists, only if it has a valid format. They allows almost all valid formats with the following exceptions: (a) they do not allow comments in the address (b) they do not allow backslash escaped quotes (c) they use the old specifications for legal domain names. We felt that allowing comments & backslash escaped quotes were extreme situations that were unnecessary to support for most uses, especially as it overly-complicates the Perl compatible regular expression used to validate the email address. These scripts were developed before the official specifications for domain names was changed to allow international characters; but we have not updated it because it works better this way with most English speaking websites. Note, though, that it does allow quoted international characters in the domain name, per the old specifications.
Below is a list of some examples of the formats these functions will validate. There are many combinations not shown in these examples, but you should be able to interpolate them.
- help@softmoon-webware.com
- Programmer <help@softmoon-webware.com>
- "Programmer: Website Developer" <help@softmoon-webware.com>
- Programmer <"help: PHP programming"@softmoon-webware.com>
- Programmer in PHP <php.help@softmoon-webware.com>
- Programmer in PHP <"PHP: programming".help@softmoon-webware.com>
- Even This Works <"strange email: formatting"@"strange domain @ host that *rocks*".com>
- more.strangeness@"strange!".subdomain."hosting @ host that *rocks*".com
- "programming, development"@softmoon-webware.com, "programming, development" <help@softmoon-webware.com>
JavaScript™ powered eMail address validation
This JavaScript™ function validates the format of an eMail address
for use in conjunction with an HTML form.
In addition, it will validate a list of email addresses, separated with commas.
It returns true
if the email address(es) are valid and false
if not.
It also creates a JavaScript™ alert (popup box) to tell the user
the email did not validate.
The example code shows how to use this function in two different ways with an HTML form.
It’s implementation is simple. It may be linked into the form control and the form submit as shown. Try the example form input; there is no submit button, but you will be hounded with alerts until you type in an email with a valid format.
<script type="text/javascript">
function checkEmail(eadd) {
eadd=eadd.replace(/^[\s]+/, ""); eadd=eadd.replace(/[\s]+$/, "");
if (eadd=="") {alert('Please enter an eMail address'); return false;}
const
literal="[\x2D^!#$%&'*+/=?0-9A-Z_`a-z{|}~]+",
quoted='"[\x20-\x7F]+"';,
word="("+literal+"|"+quoted+")",
domn="([-0-9A-Z_a-z]+|"+quoted+")",
emailAdd=word+"([.]"+word+")*@("+domn+"[.])+[a-zA-Z]{2,6}",
litName="[\x2D\x20^!#$%&'*+/=?0-9A-Z_`a-z{|}~]+",
quoName='"[\x00-\x09\x0E-\x21\u0023-\uFFFF]+"[\x20\t]*',
emailFormat="("+emailAdd+"|("+litName+"|"+quoName+")<"+emailAdd+">)",
emailList=emailFormat+"(,[\x20\t\n]*"+emailFormat+")*",
emailValid=new RegExp("^"+emailList+"$");
if (emailValid.test(eadd)) return true;
else {
alert('The eMail address you entered has an invalid format. Please check it and try again');
return false; } }
</script>
<form action="validate-email_instructions.html#content"
onsubmit="return checkEmail(document.getElementById('emailInput').value);">
<label>Please enter a valid email address
<input type='text' name='emailInput' id='emailInput' maxlength='255' size='37'
onblur='if (!checkEmail(this.value)) this.focus();' /></label>
</form>
PHP powered eMail address validation
This PHP function validates the format of an eMail address.
In addition, it will validate a list of email addresses, separated with commas (see the last example).
It returns a two-dimentional array of the address(es) if the
email address(es) are valid and false
if not.
The array returned is an array of arrays, with each second-level array containing two string values;
these values are [0] "The Complete Name Given <destination@domain.tld>"
and
[1] "destination@domain.tld"
.
We have packaged this with our companion JavaScript™ function that validates the email formatting in the user’s browser. Click to view the highlighted source code, or visit our download page.