JavaScript Email Validation

12th June 2005

Recently I have had the pleasure of working with some really well put together software from a company renowned for being at the forefront of the evolution of business process outsourcing in the UK. In the process I found they had included a wonderful little regular expression for validating an email address.

Commonly email validation in JavaScript goes little further that checking for the presence of an @ symbol, maybe even a . if you’re lucky. However calling on the power of the RegExp object it should be possible to create something a little more rigorous. Well you’d think anyway. Here’s the script,

var r, re;
var re=new RegExp("@","i");
r = document.regForm.rEmail.value.search(re);
if (r == "-1")
{
    alert ("Please enter a valid email address")
}

Impressive. I’d be grateful if someone could point out how this would be preferable to a simple indexOf, or why if you went to the trouble of using a regular expression you wouldn’t do something a little better. On the plus side it’s unlikely to inadvertently reject any valid addresses.

Permalink. Posted on 12th June 2005 in JavaScript.

Comments

There are currently no comments for this item.

Sorry, comments for this item are currently closed.

Of Interest

Hangouts

Listening