Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

javascript - Regex: Email validation that allows only hyphens in the middle of the domain and top-level domain

I know this is been asked tons of times before , but I haven't found anything that really meets all the conditions that an email address must meet to be considered valid.

Considering the following as the structure of an email address :

[email protected]
  • part1=username

  • part2=domain

  • part3 and part4 =top-level domain

These are all the conditions that must be met:

  1. An email address must not accept white spaces
  2. An email address must not end in a dot or a character other than a letter or a number
  3. Only one @ sign is allowed
  4. There can not be a special character before or after the at sign
  5. There can not be a special character before or after the domain dot (the dot after part2 of the email address)
  6. You can not enter two or more dots in a row in the username
  7. In the domain , between @ and the dot, the characters that are next to the @ and the dot must be a letter or number, in the middle the only special character allowed is the hyphen.
  8. The same in step 7 goes for the top-level domain(part 3 and part 4 or the email)

This is the regular expression I currently using :

^([w.-]+)@([w-]+)((.(w){2,9})+)$

But it does not meet conditions :4,5,6,7 and 8

I'm just trying to figure out how to complement my regular expression and learn in the process.

EDIT

The only special characters allowed in the email address are : dots, hyphens,underscores and the at sign

Here's a list of invalid emails

mkyong – must contains “@” symbol

[email protected] – domain can not start with dot “.”

mkyong()*@gmail.com – email’s is only allow character, digit, underscore and dash

mkyong@%*.com – email’s tld is only allow character and digit

[email protected] – double dots “.” are not allow

[email protected] – email’s last character can not end with dot “.”

mkyong@[email protected] – double “@” is not allow

[email protected] -email’s tld which has two characters can not contains digit

Valid:

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

[email protected]

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the best I have been able to do as per your list of valid and invalid email addresses:

^([w-]|(?<!.).)+[a-zA-Z0-9]@[a-zA-Z0-9]([w-]+)((.([a-zA-Z]){2,9})+)$

DEMO

Updated:

^([w-]|(?<!.).)+[a-zA-Z0-9]@[a-zA-Z0-9]([a-zA-Z0-9-]+)((.([a-zA-Z]){2,9}){0??,2})$

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...