-4

Do this Javascript Regular Expression to be compatible with PHP:

^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$

This regular expression works well for validate an email, and is the result (sValidEmail) of this function in javascript:

function validateMail( sEmail )
{
    // RFC822
    var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
    var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
    var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
    var sQuotedPair = '\\x5c[\\x00-\\x7f]';
    var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
    var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
    var sDomain_ref = sAtom;
    var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
    var sWord = '(' + sAtom + '|' + sQuotedString + ')';
    var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
    var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
    var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
    var sValidEmail = '^' + sAddrSpec + '$'; // as whole string
  alert( sValidEmail );
    var reValidEmail = new RegExp(sValidEmail);

    if (reValidEmail.test(sEmail)) {
        return true;
    }

    return false;
}

Thanks!

CRISHK Corporation
  • 2,948
  • 6
  • 37
  • 52
  • Enclose in regex delimiters and single quotes to use it with preg_match. But also consider just utilizing FILTER_VALIDATE_EMAIL instead. – mario May 22 '11 at 17:30
  • There is no good regex for validating emails, the RFC is too complicated. See [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) – André Paramés May 22 '11 at 18:28

1 Answers1

3

Why in the world would you use escape sequences to validate an e-mail address???

Here's one that you could use instead, and yes it can be used in both JavaScript and PHP:

[a-zA-Z0-9_\+\-]+(\.[a-zA-Z0-9_\+\-]+)*@[a-zA-Z0-9_\+\-]+(\.[a-zA-Z0-9_\+\-]+)*\.[a-zA-Z]{2,}
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • That doesn't work - it doesn't match an address with `+`, as in `test+hello@gmail.com`. – André Paramés May 22 '11 at 18:26
  • @André Paramés: Sorry, fixed (but if you ask me, people with that kind of e-mail address are asking to be rejected - even Facebook does it :-) ) – Ry- May 22 '11 at 18:32
  • the "+suffix" notation is mainly used to filter emails depending on the source; I might use `myemail+stackoverflow@provider.com` and then I can filter all emails coming from SO. It's also useful to know where does spam come from, since you know exactly what website gave spammers your address. – André Paramés May 22 '11 at 18:46