1

I currently use this pattern for checking email format:

/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i

But it is not the same format with Facebook's pattern.

For example: email@abc.co.jp.jp is incorrect but it's valid to the pattern above. I need a pattern follow facebook format.

hungneox
  • 9,333
  • 12
  • 49
  • 66
  • 1
    Is it your intention to validate email addresses correctly, or validate the way Facebook does? (Not that I'm saying Facebook is wrong, but these are two distinct questions.) Possible duplicate of [What is the best regular expression for validating email addresses?](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses) – nnnnnn Dec 28 '11 at 07:21
  • @nnnnnn I wnat to validate the way facebook does :) – hungneox Dec 28 '11 at 07:23
  • Ah, so blindly follow Facebook whether it is right or wrong? In that case either (a) Facebook does it in JS so you should look through their code to find their regex (or other method), or (b) Facebook does it server-side in which case there's no way to see how they do it. You're better off with a pattern that is more permissive, i.e., might let some invalid addresses through, then one that is too restrictive, i.e., might reject some valid addresses. – nnnnnn Dec 28 '11 at 07:27
  • @nnnnnn I'm currently developing Facebook app, so I think I should follow their style – hungneox Dec 28 '11 at 07:29
  • If a user wants to enter an invalid address, nothing will stop her/him. If regex is to be used it should be there simply to let the user know in case s/he made some obvious typo, not to force them to be more creative with the email address. – Qtax Dec 28 '11 at 07:56
  • 1
    Eh? `email@abc.co.jp.jp` _IS_ a valid address! If you want real email validation, do it on the server side -- particularly, in Javascript you certainly won't be able to query MX entries – fge Dec 28 '11 at 10:22

1 Answers1

4

Facebook signup page has a script that defines function is_email as:

function is_email(a){return /^([\w!.%+\-])+@([\w\-])+(?:\.[\w\-]+)+$/.test(a);}

Not quite RFC822: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html

Mischa Arefiev
  • 5,227
  • 4
  • 26
  • 34
  • This probably has to do with the fact, that only a subset of the special characters, mentioned in RFC822 can be used unencoded in a url. According to RFC 1738 - Uniform Resource Locators (URL): *Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.* – Alexander Popov Oct 18 '16 at 11:30
  • It doesn't look like actual regex for validation on the Nov of 2017 by Facebook. – Oleksandr Yefymov Nov 08 '17 at 12:45