Skip to content

Commit

Permalink
Improve email validation (Issue luddep#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
MacFJA committed Apr 28, 2013
1 parent d7086e1 commit 0a523c8
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions LPEmail.j
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

@import <Foundation/CPObject.j>

var emailPattern = new RegExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$");

@implementation LPEmail : CPObject
{
CPString email;
Expand All @@ -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
Expand Down

0 comments on commit 0a523c8

Please sign in to comment.