JavaScript: The Utlimate RegExp Email Address Format Validator

(Originally posted on the “old” Jason Bennett’s Developer Corner, Thursday, September 18, 2008)

I developed the following email address format validator for my current ADF Faces project.  The QA person keep failed my feeble attempt at a validator, so I decided to go big guns and create a validator that conforms to the standards for email addresses as set forth in this wiki entry:http://en.wikipedia.org/wiki/E-mail_address .  I created a single Regular Expression to handle all cases (unless someone can break it).  Here is the function:

(Make sure you put the regexp in one long string if you cut and paste this.  It wrapped in the blog entry.)

function isValidEmail(ctl){

v_pattern = /^((([^.]*)(([a-zA-Z0-9#!%/-=_`~&’$\*?\|^\{}\+][.]{0,1})+)[^.]|(“.*?”))[@](([a-zA-Z0-9-]+)([.]([a-zA-Z]{2,3}))+|([[]{1}(([0-1]?[0-9]{1,2}.)|(2[0-4][0-9].)|(25[0-5].)){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))[]]{1})))$/;


if(ctl.length > 0){
if (ctl.match(v_pattern)){
return true;
}else{
return false;
}
}else{
return true;
}
}

Comments are closed.