regular expression for email format

I have a function that evaluates whether an input matches a valid email format, but it is horribly slow.  Any ideas?

The validation it needs to run is:
start with valid name characters (A-Za-z0-9._) (am I missing any?)
then an @
then valid characters (A-Za-z0-9._) (am I missing any?)
then a .
then 2-4 characters
function isValidEmailFormat(strEmail){
//alert(strEmail);
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,2})+$/.test(strEmail)){
        return true;
    } else {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmail)){
            return true;
        } else {
            if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(strEmail)){
                return true;
            } else {
                return false;
            }
        }
    }
    return false; 
}

Open in new window

LVL 18
nap0leonAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
moagriusConnect With a Mentor Commented:
Here is a pretty sturdy email validation expression.  it'll include more complex emails containing multiple dots and odd domains.
var isemail = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
 
//  examples
var good = "bob@bob.com";
var bad = "bob@bob";
var complex = "jj.abrams@nbc.universal.com";
alert(isemail.test(good));
alert(isemail.test(bad));
alert(isemail.test(complex));

Open in new window

0
 
nap0leonAuthor Commented:
Appears to work great - thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.