最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 plement 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 – email’s is only allow character, digit, underscore and dash

mkyong@%* – 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]

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 plement 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. – email’s is only allow character, digit, underscore and dash

mkyong@%*. – 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]

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked May 22, 2014 at 2:56 AxelAxel 1,6744 gold badges26 silver badges39 bronze badges 5
  • /[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g from regexr./2rhq7 – hjpotter92 Commented May 22, 2014 at 3:01
  • What language are you using? – hwnd Commented May 22, 2014 at 5:41
  • javascript. I also wanted to validate in the server with C# , but I think javascript will be more than enough – Axel Commented May 22, 2014 at 5:46
  • You asked for my help some time ago, can you please provide a set of valid and invalid email addresses that I can test my regex against. Thanks – sshashank124 Commented May 22, 2014 at 7:07
  • @sshashank124 I added a few examples to my question, please take a look – Axel Commented May 22, 2014 at 12:18
Add a ment  | 

3 Answers 3

Reset to default 5

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})$

An email address must not accept white spaces

No, "this is"@a-valid-e-mail-address..

Only one @ sign is allowed

No, "this@is"@a-valid-e-mail-address..

There can not be a special character before or after the at sign

I’m not sure what this means, but it’s probably not true. !#$%&'*+-/=?^_`{}|[email protected] is perfectly fine.

There can not be a special character before or after the domain dot (the dot after part2 of the email address)

does@ţḩıš.çóûñţ? Because it’s a valid e-mail address.

You can not enter two or more dots in a row in the username

can(...)too@localhost

Don’t use regular expressions to validate e-mail addresses, please.

Email addresses do have a specific format that can be represented as a regular expression. The official format from RFC 5322 is:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]      |  \\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@ (?:(?:a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?  |  \[(?:(?:25[0-5]|2[0-]0-9]|[01]?[0-9][0-9]?)\.){3}       (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-]:          ?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

As indicated by Jan Goyvaerts, regular expressions don't send emails and therefore you can't actually know that it isn't valid until the email is sent and not received by the recipient.

Since you are tweaking the email regular expression in your own way, it doesn't make it right. It may answer your requirements to use that format, but email is fixed format and therefore just use the reference one, or a simplified one such as those listed on the Regular-Expressions page. Otherwise, this question will never get answered to your exact requirements.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论