From 0a523c83b848210a3e397ab69c3f3465238e2ec8 Mon Sep 17 00:00:00 2001 From: MacFJA Date: Sun, 28 Apr 2013 16:24:08 +0200 Subject: [PATCH] Improve email validation (Issue #1) Should follow the RFC 5322 (http://tools.ietf.org/html/rfc5322#section-3.4) Return true for: "John Doe" John john.doe@example.com Return false for: John Doe john.doe.@example.com john.doe@example.com> --- LPEmail.j | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) mode change 100644 => 100755 LPEmail.j diff --git a/LPEmail.j b/LPEmail.j old mode 100644 new mode 100755 index 6057f61..a01e5f9 --- a/LPEmail.j +++ b/LPEmail.j @@ -30,8 +30,6 @@ @import -var emailPattern = new RegExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$"); - @implementation LPEmail : CPObject { CPString email; @@ -44,7 +42,26 @@ var emailPattern = new RegExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$") + (BOOL)emailWithStringIsValid:(CPString)anEmail { - return emailPattern.test(anEmail); + var localChar = "[a-zA-Z0-9!#$%&'*+/=?^_`{}|~-]", + domainChar = '[\x20-\x5A\x5E-\x7E]', + emailAddressRegEx = localChar + '+(\\.' + localChar + '+)*@' + domainChar + '+(\\.' + domainChar + '+)?\\.[a-zA-Z]{2,}'; + + var quoteEmail = new RegExp('^".[^"]+"\\s*<' + emailAddressRegEx + '>$'), + noQuoteEmail = new RegExp('^([!\x23-\x5A\x5E-\x7E]+\\s*)?<' + emailAddressRegEx + '>$'), + onlyAddress = new RegExp('^' + emailAddressRegEx + '$'); + + if(quoteEmail.test(anEmail)) + { + return true; + } + else if(noQuoteEmail.test(anEmail)) + { + return true; + } + else + { + return onlyAddress.test(anEmail); + } } - (void)initWithString:(CPString)anEmail