Check Email Address with JavaScript and Regular Expressions
Douglas Karr at 5:16 pmThanks for stopping by my personal blog on Marketing Technology! Over 50,000 visitors a month find my content worth returning for, so don't forget to subscribe to the Marketing Technology Blog RSS feed or to the Marketing Technology Email to have new content sent directly to your inbox. You may also find my other business blog helpful, Social Media Domination.
A while ago I put up a Password Strength Checker using JavaScript and Regular Expressions. On that same note, you can also check the structure of an email address utilizing the same methodology:
If your form element has the id=emailaddress and you add a form onSubmit=”return checkEmail();”, this is a Javascript function that you can utilize to return an alert if the email address has a valid structure or not:
<script language="javascript">
function checkEmail() {
var email = document.getElementById(’emailaddress’);
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert(’Please provide a valid email address’);
email.focus
return false;
}
}
</script>
The function also returns the focus back to the emailaddress field!


var valid = true;
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
$$(’.emailaddress’).each( function(email) {
if (!filter.test(email.value)) {
alert(’Please provide a valid email address’);
email.focus;
valid = false;
}
});
return valid;
For an example of a regular expression that does a decent job alongside an explanation of which cases it does not cover, see this:
http://www.regular-expressions.info/email.html
My personal preference is to cover most of the simple cases and issue a warning for everything else rather than rejecting it. If Bob really want sto submit bob@com.museum rather than bob@museum.com, why not let him?
You can test out the Regex utilizing an Online Regex Tester.
Also, there’s definitely much more that can be done if you want to ensure an email address is valid in accordance with the RFC.
There are a few reasons not to allow someone to enter an invalid email address:
1. They will get annoyed at you when the email they expected doesn’t get through - regardless of whether or not it was your fault the address was entered incorrectly.
2. If com.museum was a valid domain and, let’s say, Yahoo! operated it - any email address that bounced would have a negative impact on your company’s reputation for email delivery. This could lead to all of your company’s email being blocked.
3. If your email service provider allowed you to enter bob@com.museum, you’d also pay for each email sent to that email address until they unsubscribed that address due to bounces. I would steer clear of any ESP that would allow an invalid email address like that - they’re just taking your money!
Thanks for stopping by!
Doug
var regex = /^[a-z0-9\._-]+@([a-z0-9_-]+\.)+[a-z]{2,6}$/i;- With the final modifier /i there’s no need to indicate the upper case range.
- I don’t know of any TLD with numbers in it.
On a side note, I do allow TLD with up to 6 chars; new ones arrive regularly and you never know (well, somme future ones may even have numbers in it, I know).
I am tring to use this in an existing form in real-time, but this doesnt appear to be validating in realtime like your password strength checker…
Or, am i just that clueless, and it aint workin for me?