#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    1
    Rep 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.
  2. #2
  3. Cunning Linguist
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    I used to live at home, now I stay at the house
    Posts
    10,180
    Rep Power
    2039
    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.
  4. #3
  5. Cunning Linguist
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2003
    Location
    I used to live at home, now I stay at the house
    Posts
    10,180
    Rep Power
    2039
    You might also find that if you search this forum, someone might have
    had a similar problem and posted a solution.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    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.
  8. #5
  9. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Sep 2007
    Location
    outside Washington DC
    Posts
    2,642
    Rep Power
    3700
    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.
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    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.
    http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

    Comments on this post

    • ManiacDan agrees : mind.boggle()
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Sep 2007
    Location
    outside Washington DC
    Posts
    2,642
    Rep Power
    3700
    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
  14. #8
  15. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,131
    Rep Power
    4304
    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})$

    Comments on this post

    • ManiacDan agrees : I swear I thought I mentioned plus addressing.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo