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

[email protected]%*.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 “.”

[email protected]@gmail.com – 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]

Answers:

Answer

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

An email address must not accept white spaces

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

Only one @ sign is allowed

No, "[email protected]"@a-valid-e-mail-address.com.

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)

[email protected]???š.çóûñ?? Because it’s a valid e-mail address.

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

can(...)[email protected]

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

Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.