Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Created November 29, 2010 10:32
Show Gist options
  • Save badsyntax/719800 to your computer and use it in GitHub Desktop.
Save badsyntax/719800 to your computer and use it in GitHub Desktop.

Revisions

  1. badsyntax revised this gist Nov 29, 2010. 1 changed file with 3 additions and 18 deletions.
    21 changes: 3 additions & 18 deletions email_validation.js
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,6 @@
    // See http://rosskendall.com/blog/web/javascript-function-to-check-an-email-address-conforms-to-rfc822

    String.prototype.isEmail = function(){
    function isEmail(email){

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

    return new RegExp(sValidEmail).test( this );
    };
    return /^([^\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))*$/.test( email );
    }
  2. badsyntax created this gist Nov 29, 2010.
    21 changes: 21 additions & 0 deletions email_validation.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    // See http://rosskendall.com/blog/web/javascript-function-to-check-an-email-address-conforms-to-rfc822

    String.prototype.isEmail = function(){

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

    return new RegExp(sValidEmail).test( this );
    };