Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to validate International email

Not sure if there is a solution available but not able to find. So asking it again.

I am writing an email validator. This should validate all well formed email(This is just one level of validation to check the email is well formed). Now as my code is an international code so i should support non latin characters also. How do i write the most efficient Regex for that?

International Emails: http://en.wikipedia.org/wiki/International_email

Some example emails:

  1. 伊昭傑@郵件.商務
  2. राम@मोहन.ईन्फो
  3. юзер@екзампл.ком
  4. θσερ@εχαμπλε.ψομ

It should be able to validate all the above formats

like image 473
KD. Avatar asked May 09 '13 05:05

KD.


People also ask

Can email be validated with regex?

Regex provides the ability to validate the structure of an email address.

What is regex validation in email?

[a-zA-Z0-9+_. -] matches one character from the English alphabet (both cases), digits, “+”, “_”, “.” and, “-” before the @ symbol. + indicates the repetition of the above-mentioned set of characters one or more times. @ matches itself.

What is the best email regex?

The best all-around regex to find valid email addressesallows Latin characters ("a" - "z" or "A" - "Z") within the email address. permits digits (0 - 9) in the email address. enforces domain part restrictions. allows hyphens (-) inside the domain as long as they don't lead or trail the domain.


2 Answers

The reason why email validation through regexp is so lax is because it's not efficient. There is a spec for email address syntax, but the regexp to check it is so long, it's impractical. Besides, email providers are more strict in their implementation of the syntax than the actual spec. An email might be considered valid as the spec says it is, but invalid according to the provider.

That's also the reason why activation emails exist, because the only way to check if an email is valid, existing and currently in use is to send it something, usually a unique activation code or link. Only when that unique activation code or link, only sent to that email, is used will the email be considered valid.

Until then, consider a more lax approach in validating emails, checking if the username, the @ and the domain parts. Besides, why would one sign up using a false email anyway? If they did, they wouldn't get the activation link, and can't proceed creating the account.

like image 147
Joseph Avatar answered Sep 27 '22 18:09

Joseph


@Patashu Thanks a lot. I've improved your regex a bit and now it's absolutely suits my needs:

^([^@\s\."'\(\)\[\]\{\}\\/,:;]+\.)*[^@\s\."'\(\)\[\]\{\}\\/,:;]+@[^@\s\."'\(\)\[\]\{\}\\/,:;]+(\.[^@\s\."'\(\)\[\]\{\}\\/,:;]+)+$
like image 22
budedub Avatar answered Sep 27 '22 19:09

budedub