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
LVL 1
slightlyoffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

Maybe

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

Regards
0
Anthony FitzgeraldVP Technology & LocalizationCommented:
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.
0
slightlyoffAuthor Commented:
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!!!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Anthony FitzgeraldVP Technology & LocalizationCommented:
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.
0
slightlyoffAuthor Commented:
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.
0
Anthony FitzgeraldVP Technology & LocalizationCommented:
Its the correct syntax. Unless it just doesn't fit well with JavaScript.

I have saved it here on RegExr, so you can see it working.



For some reason you can't see my web links in these comments.
Have a look here: regexr.com/3bs6m

Also this is cool site for testing your RegEx, you can play around with it.


Cheers,
Anthony.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony FitzgeraldVP Technology & LocalizationCommented:
Well, how did you get on? Did you get it working in the end?
0
slightlyoffAuthor Commented:
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
0
käµfm³d 👽Commented:
Personally, I'd use the pattern found here:

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

...for the reasons the author outlines.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

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.