admin管理员组文章数量:1356084
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:
- An email address must not accept white spaces
- An email address must not end in a dot or a character other than a letter or a number
- Only one @ sign is allowed
- There can not be a special character before or after the at sign
- There can not be a special character before or after the domain dot (the dot after part2 of the email address)
- You can not enter two or more dots in a row in the username
- 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.
- 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:
- An email address must not accept white spaces
- An email address must not end in a dot or a character other than a letter or a number
- Only one @ sign is allowed
- There can not be a special character before or after the at sign
- There can not be a special character before or after the domain dot (the dot after part2 of the email address)
- You can not enter two or more dots in a row in the username
- 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.
- 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
3 Answers
Reset to default 5This 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.
本文标签:
版权声明:本文标题:javascript - Regex: Email validation that allows only hyphens in the middle of the domain and top-level domain - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743940254a2565430.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论