-1

I have this regulular expression to validate email id but I can't allow two consecutive hyphen on domain, However it doesn't works for me whatever the fix I made. Could anyone please help?

/^[a-zA-Z\-0-9](([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\-\].,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

Test Id to fail case: sant@y--t.com

Please help, Thanks for your assisstance!

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
San
  • 3
  • 2
  • 1
    Don't use regex for email validation (or only for minimal checks to confirm the email looks roughly like an email). The only way to validate an email address is to send an email there and validate that the recipient got it. See http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – xxbbcc Sep 10 '14 at 01:51
  • 1
    @xxbbcc - *Verification* is not equivalent to *validation*. – Jared Farrish Sep 10 '14 at 01:53
  • @JaredFarrish I know but most massive email validator regular expressions are flawed in various ways. If _proper_ validation is needed, then the email has to be verified. Other than that, what's the point of working out a massive regex that may fail in subtle ways? – xxbbcc Sep 10 '14 at 01:55
  • Typically, any client-side validation is meant only as a service for the user for basic errors that will be rejected by the server's validation. Warning someone that an "invalid" email was entered is useful if someone entered a blatantly incorrect string *by accident*. Verifying the account (if at all necessary) is a separate and later step. – Jared Farrish Sep 10 '14 at 01:57
  • @JaredFarrish One more thing: what's the point of validating an email address unless you know that the person entering it is the real owner? Validating the email address without verifying it tells you nothing. For simple validation a trivial regex is typically enough. – xxbbcc Sep 10 '14 at 01:58
  • 2
    Why? That's a valid domain. –  Sep 10 '14 at 02:12

1 Answers1

1
^[a-zA-Z\-0-9](([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\-\].,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z0-9](?:[a-zA-Z0-9]|-(?!-))*\.)+[a-zA-Z]{2,}))$

Demo


Change:

([a-zA-Z\-0-9]+\.)

To:

([a-zA-Z0-9](?:[a-zA-Z0-9]|-(?!-))*\.)

Match one [a-zA-Z0-9], and then loop through [a-zA-Z0-9] or - (as long as it isn't followed by another -) repeated 0+ times.

Sam
  • 20,096
  • 2
  • 45
  • 71
  • For the record, I agree with the comments on the OP. – Sam Sep 10 '14 at 01:53
  • Now it fails for this case email@-gmail.com whihc is invalid - domian cannot start with - – San Sep 24 '14 at 15:53
  • @San updated with a negative lookbehind for a `@` before the `-` in domain. Please note that there is a long RFC spec for email addresses and domains and this expression probably doesn't take it all into account. But I have modified your original expression to disallow duplicate `-` in domains and disallow a domain starting with `-`. As always, I'd appreciate an upvote for my time :) – Sam Sep 24 '14 at 16:01
  • I can surely give you an good upvote, Howevetr if I use the symbol '<' my regular expression becomes invalid in my code and it not compiling. Not sure is thefre any alternative???? – San Sep 24 '14 at 17:02
  • Sorry, I didn't realize this was Javascript (since I'm just coming back to an old answer) which doesn't support negative lookbehinds. Back to the drawing board! – Sam Sep 24 '14 at 17:48
  • @San updated. Less attractive answer without the negative lookbehind, but pretty much just make sure the first character is alphanumeric and then do our hyphen loop 0+ times (rather then doing the hyphen loop 1+ times). – Sam Sep 24 '14 at 17:50