Deepak Negi
Deepak Negi

Reputation: 97

Email Subdomains cannot start or end with a hyphen, nor have two in a row

I have a situation in which email domain need to pass below scenarios

Domain:

Not Accepted subdomain

Accepted

Example:

Email- [email protected]
Username - foobar
Domain - mahi.co.in


var emailIdExp = ^[A-Za-z0-9._-]+@([A-Za-z0-9-]+\.)*[A-Za-z0-9-]+\.[A-Za-z0-9-]{2,}$

Upvotes: 0

Views: 755

Answers (2)

Narbhakshi
Narbhakshi

Reputation: 392

You can try with:

^[\w.-]+@[A-Za-z0-9]+([-]?[A-Za-z0-9]+)+([\.][A-Za-z0-9]+([-]?[A-Za-z0-9]+)+)+$

The above regex is updated here : https://regex101.com/r/7P4XWM/3

Hope it helps :)

Upvotes: 1

lpg
lpg

Reputation: 4937

You can try with that regex:

^[\w-_\.]+@\w+(-\w+)*(\.\w+(-\w+)*)+$

Example: https://regex101.com/r/7P4XWM/1

With that, the following condition is not fulfilled, though:

  • Each subdomain must have at least 2 characters.

Upvotes: 1

Related Questions