Link to home
Start Free TrialLog in
Avatar of slightlyoff
slightlyoff

asked on

Regular Expression - Allow multiple periods in emails

I have the following regular expression:
function regIsEmail(fData){ 
				var reg = new RegExp("^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$"); 
				return reg.test(fData); 
	}

Open in new window


the problem is, this expression does not allow email addresses like: firstName.lastName@school.grade.state.us
An email in this format gets rejected.  I personally would reject it for being annoying, but that's not the point.

What do I need to change to make this work?

Thanks for your help!!!

-Steve
Avatar of Rgonzo1971
Rgonzo1971

Hi,

Maybe

var reg = new RegExp("^[0-9a-zA-Z]+@(?:[A-Z0-9-]+\.)+?[0-9a-zA-Z]+$");

Regards
Hi,

There are probably a few different ways. How about using the look ahead and look behind to check that they don't start or end with a period and then just allow any numbers/characters and period, something like this:

var reg = new RegExp("^(?!\.)[0-9a-zA-Z\.]+(?<!\.)@(?!\.)[0-9a-zA-Z\.]+(?<!\.)$");

Cheers,
Anthony.
Avatar of slightlyoff

ASKER

Hello!  Thank you for your replies!  I tried the second code first from Anthony - something is wrong with that one, it breaks the javascript and the form won't submit.

I tried the one from Rgonzo1971 next, and that one won't let the periods in the email.  I tried using: steve.me@sd.k12.us

I get invalid email.

Any advice?  thanks again for your help!!!
My RegEx expression works fine when I tested it.
But of course you are using JavaScript. RegEx Look Behind is not supported in JavaScript. That sucks.

Try this without the Look Behind instead. I haven't tested it in JavaScript, but I have tested it in another tool and it works.

var reg = new RegExp("^(?!\.)[0-9a-zA-Z\.]+[0-9a-zA-Z]@(?!\.)[0-9a-zA-Z\.]+[0-9a-zA-Z]$");

Let me know how you get on.

Cheers,
Anthony.
Thanks again for your help!  After putting in the new Regex, it triggers an invalid email when i try steve@mycompany.usa.com.  With my original regex expression, I can get email's like this to work,  but the moment i try first.last@mycompany.usa.com it doesn't work.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Fitzgerald
Anthony Fitzgerald
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, how did you get on? Did you get it working in the end?
Sorry for the delay - they had me on other projects, and i finally got back to it.
It's working now.  People need to have more simple email addresses in my humble opinion.

Thanks again!

-Steve
Personally, I'd use the pattern found here:

http://www.regular-expressions.info/email.html

...for the reasons the author outlines.