-8

Can anyone help me out with validation of Email in iOS for emails like "0@0.bk", "0@0.ak.bk.ck.dk.ek.fk.gk.hk.lk.mk.nk.ok.pk", etc.,? Testers in our office are asking validation of emails like these. I would appreciate any sort of help.

tehdoommarine
  • 1,868
  • 3
  • 18
  • 31
  • What exactly does it mean to help with validation of e-mails? – zoul Sep 11 '12 at 12:43
  • 2
    See [the first answer to this question](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address). Essentially: the only way to "validate" it is to successfully use it. – Russ Amos Sep 11 '12 at 12:43
  • Welcome to Stack Overflow. Please take time to read the [FAQ](http://stackoverflow.com/faq) and please use the SEARCH function to check if your question has been asked previously (MANY MANY times....) – Nick Bull Sep 11 '12 at 12:50
  • 1
    *MANY, MANY, MANY, MANY, MANY* – dan-lee Sep 11 '12 at 12:51
  • You should ask your `testers` to show a single `email id` with this format!!! – Hemang Sep 11 '12 at 13:12

2 Answers2

5

Please don't try to validate emails. It's very complicated. If you want to check if an email is valid, send an email to it and get the user to respond by, eg, clicking a link.

Doing more than checking for the presence of an "@" and a "." is probably not helpful.

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
0
 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;
        predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegEx];


        NSString *yourString = @"abc.yahoo.co.in";
        if ([predicate evaluateWithObject:yourString]) {
          NSLog (@" valid email");
      }else
    {
        NSLog(@"Not a valid email");
    }
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
  • This does nothing to check if the email is "valid". This just checks if the email is formatted correctly (in a hideously overly complex way!). Do you seriously use that regex in production code? – Nick Bull Sep 11 '12 at 12:58