Reputation: 3473
I want to validate email id :
[email protected] :-- Valid
[email protected] : invalid
As per my knowledge multiple dots are not allowed after "@" in email id. Please advice how can i detect the more the one dots after @ and and check the validation.
I am using below regex for email validation. both are working fine. But not able to check multiple dots after @ symbols.
NSString *emailRegex =
@"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
@"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
@"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
@"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
@"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
@"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
@"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", emailRegex];
return [emailTest evaluateWithObject:email];
I also tried this :
NSString *regex1 = @"\\A[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,4}\\z";
NSString *regex2 = @"^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*";
NSPredicate *test1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];
NSPredicate *test2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex2];
return [test1 evaluateWithObject:email] && [test2 evaluateWithObject:email];
Upvotes: 0
Views: 2549
Reputation: 4097
The expression, that you are using, satisfies the RFC 5322. Multiples dots after @ are perfectly valid for hostname. How other user you tell you if you limit to one dot, you were blocking many domains.
+ (BOOL) validateEmail: (NSString *) candidate;
{
// The Official Standard: RFC 5322
// http://www.regular-expressions.info/email.html
NSString *emailRegEx =
@"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
@"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
@"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
@"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
@"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
@"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
@"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
return [predicate evaluateWithObject:candidate];
}
Upvotes: 1
Reputation: 3417
For : [email protected]
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
For exclude check multiple dots after @ symbols. [email protected] :-- Valid
[email protected] : invalid
exclude dot from @[A-Za-z0-9.-]
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
Upvotes: 0
Reputation: 793
Please try this
+ (BOOL)isValidEmail: (NSString *)email {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
Upvotes: 2
Reputation: 29764
Don't use a regex for this, use NSDataDetector:
NSError *error = nil;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
if (!detector) { /* handle error */ }
You can then use detector
the same as you would an NSRegularExpression
:
NSString *email = @"[email protected]";
NSTextCheckingResult *match = [detector firstMatchInString:email options:0 range:NSMakeRange(0, email.length)];
NSLog(@"%@", match);
There are many hidden pitfalls to matching well-known types like this via regex (as you build in assumptions about what is valid, as you have in your question; subdomains and multi-level TLDs are certainly valid); avoid doing it yourself where possible.
Upvotes: 3