slightlyoff
asked on
Regular Expression - Allow multiple periods in emails
I have the following regular expression:
the problem is, this expression does not allow email addresses like: firstName.lastName@school. grade.stat e.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
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);
}
the problem is, this expression does not allow email addresses like: firstName.lastName@school.
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
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.
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\
Cheers,
Anthony.
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!!!
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-z A-Z]@(?!\. )[0-9a-zA- Z\.]+[0-9a -zA-Z]$");
Let me know how you get on.
Cheers,
Anthony.
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\
Let me know how you get on.
Cheers,
Anthony.
ASKER
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.c om it doesn't work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, how did you get on? Did you get it working in the end?
ASKER
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
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.
http://www.regular-expressions.info/email.html
...for the reasons the author outlines.
Maybe
var reg = new RegExp("^[0-9a-zA-Z]+@(?:[
Regards