0

I want to validate email id :

abc.pqr@companyname.com :-- Valid

abc.pqr@xyz.companyname.com : 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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
ios developer
  • 3,363
  • 3
  • 51
  • 111
  • 2
    Why wouldn't multiple dots be allowed? It is perfectly valid for hostname to have many dots. If you try to limit them you'll have quite many angry people, like every company in the UK (company.co.uk)... – Sami Kuhmonen Mar 08 '17 at 06:08
  • Possible duplicate of [Check that an email address is valid on iOS](http://stackoverflow.com/questions/3139619/check-that-an-email-address-is-valid-on-ios) – Antony Raphel Mar 08 '17 at 06:51
  • Check [sign in validations](http://www.programmingcrew.in/2015/09/sign-in-validations-in-objective-v.html) – pkc456 Mar 14 '17 at 08:58

4 Answers4

3

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 = @"my@email.com";        
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.

Itai Ferber
  • 28,308
  • 5
  • 77
  • 83
2

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];
 }
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
Pooja Gupta
  • 785
  • 10
  • 22
1

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];
}
93sauu
  • 3,770
  • 3
  • 27
  • 43
0

For : abc.pqr@companyname.com

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. abc.pqr@companyname.com :-- Valid

abc.pqr@xyz.companyname.com : 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];
Sid Mhatre
  • 3,272
  • 1
  • 19
  • 38