Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

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

0
nap0leon
Asked:
nap0leon
1 Solution
 
moagriusCommented:
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now