The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Email Regular expression
Discuss Email Regular expression in the Regex Programming forum on Dev Shed. Email Regular expression Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 26th, 2009, 11:09 AM
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 1
Time spent in forums: 36 m 10 sec
Reputation Power: 0
|
|
|
Email Regular expression
Hi All,
I am using following regular expression to validate email addresses but somehow it is not validating emails for hyphen or dashes (-).
this email is not valid with following regex: email-email @ email .gov (put spaces as forum blocks email addresses.)
var emailpattern=new RegExp("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z0-9-]+)");
Any idea what am i missing even when included hyphen.
Any help would be really appreciated.
|

February 26th, 2009, 11:37 AM
|
 |
Cunning Linguist
|
|
Join Date: Jul 2003
Location: I used to live at home, now I stay at the house
|
|
You may be implementing this with JS, but it's more of a general RegEx
question, I think.
I've moved it to the RegEx forum where it should get some attention.
If you have trouble implementing the final solution in your JS, just pop
back into the JS forum and someone should be able to help.
--Jon.
|

February 26th, 2009, 11:38 AM
|
 |
Cunning Linguist
|
|
Join Date: Jul 2003
Location: I used to live at home, now I stay at the house
|
|
You might also find that if you search this forum, someone might have
had a similar problem and posted a solution. 
|

February 26th, 2009, 04:04 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Try this expression on for size:
Code:
^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
Also, that expression you posted DID match email-email@email.gov for me in PHP. No edits aside from the escapes on your backslashes. Are you sure you're doing them right?
-Dan
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

February 26th, 2009, 06:08 PM
|
 |
Contributing User
|
|
Join Date: Sep 2007
Location: outside Washington DC
|
|
|
Just a warning, the real parsing of email addresses, as specified by RFC822, is really ugly. While its probably possible to do it all with a regex, I've never seen one that is perfect.
But a quick search will get one that is good enough for real world use.
|

February 26th, 2009, 06:35 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

February 26th, 2009, 06:57 PM
|
 |
Contributing User
|
|
Join Date: Sep 2007
Location: outside Washington DC
|
|
Quote: | Originally Posted by Scorpions4ever There is a regular expression to validate any RFC 822 compliant address, but the actual expression is over 1 page long and is mind-bogglingly complex. |
I didn't know it existed. Thanks.
I can believe on the mind-boggling part
|

March 2nd, 2009, 04:48 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
Being able to use "+" in my addresses for the purpose sorting messages is useful for me. (Some people call it "plus addressing".) Unfortunately many sites don't allow addresses like that.
Code:
^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(\+[-.\w]*[0-9a-zA-Z])?@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|