Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

regular expression for email format

Posted on 2009-05-18
2
Medium Priority
?
495 Views
Last Modified: 2012-05-07
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
Comment
Question by:nap0leon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
moagrius earned 2000 total points
ID: 24412628
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
 
LVL 18

Author Closing Comment

by:nap0leon
ID: 31582619
Appears to work great - thanks!
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question