4

Email validation checking in iPhone programming can be done using RegexKitLite library (iOS2.0), NSPredicate (iOS 3.0 onwards) and NSRegularExpression (iOS 4.0). But can anybody state what is the advantage of one over the other and which is the best validating option of the three stated.

petert
  • 6,672
  • 3
  • 38
  • 46
user574089
  • 350
  • 2
  • 4
  • 12
  • FYI, it's an example of something incredibly easier on Android, it's builtin there .. if (!android.util.Patterns.EMAIL_ADDRESS.matcher(someString).matches()) – Fattie May 24 '14 at 10:35

7 Answers7

11

i am using NSPredicate always...and it is working fine

NSString *emailid = emailField.text;
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest =[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL myStringMatchesRegEx=[emailTest evaluateWithObject:emailid];
Praveen
  • 301
  • 2
  • 11
8

My answer is a refactored one from the excellent solution provided in this link..

///Returns YES (true) if EMail is valid
+(BOOL) IsValidEmail:(NSString *)emailString Strict:(BOOL)strictFilter
{
    NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";

    NSString *emailRegex = strictFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

    return [emailTest evaluateWithObject:emailString];
}
Community
  • 1
  • 1
3
- (BOOL)validateEmail:(NSString *)emailStr
{
    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:emailStr];
}

On Button Click :

if (![self validateEmail:[txtEmail text]])
    {
        alertValidator.message = @"Please Enter Valid Email Address !";
        [alertValidator show];
        txtEmail.text = @"";
        [txtEmail becomeFirstResponder];
    }
Pushkraj Lanjekar
  • 2,254
  • 1
  • 21
  • 34
2

I think the best option, hands down, is Mailgun's free email validation API. I've written a simple objective-C wrapper for it:

https://github.com/benzguo/BZGMailgunEmailValidation

BZGMailgunEmailValidator *validator = 
    [BZGMailgunEmailValidator validatorWithPublicKey:YOUR_PUBLIC_KEY];

[validator validateEmailAddress:self.emailFieldCell.textField.text
                        success:^(BOOL isValid, NSString *didYouMean) {
                        // Validation succeeded
                      } failure:^(NSError *error) {
                        // Validation failed
                      }];

If there's a connection error, the validator falls back to regex-based validation.

If you still want to validate emails using a regular expression, check out:

http://www.regular-expressions.info/email.html

https://wiki.mozilla.org/TLD_List

benzguo
  • 176
  • 1
  • 4
1

I think it largely depends on the minimum version of iOS an App will support, and if there's a built-in class that'll do the job, use it. As you hinted, you'd use some other 3rd-party framework to support older versions in a consistent way.

Right now, I'd use NSPredicate or NSRegularExpression, both are supported from iOS 4 onwards, which is quite likely to be the minimum supported version for new iOS Apps, if not iOS 5.

Useful post.

Community
  • 1
  • 1
petert
  • 6,672
  • 3
  • 38
  • 46
  • Thank you for giving solution particular to the question only. Right now i am working on iOS 5.0 and NSPredicate is working fine on it. – user574089 Dec 16 '11 at 09:32
0

complete email validation. try this

- (BOOL)validateEmailWithString:(NSString*)checkString
{

    NSString *laxString = @".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", laxString];
    return [emailTest evaluateWithObject:checkString];
}
Patel Jigar
  • 2,141
  • 1
  • 23
  • 30
-2

how to create email id and password validation in ios

Hurry up, Easy for simple Validation of Email id check that after code useful.

- (IBAction)actLogin:(id)sender

{

    if([self validateEmailWithString:userNameText.text]==YES)
    {
         NSLog(@"valid email");
         if (passWordText.text.length>6)
         {
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:userNameText.text forKey:@"username"];
            [defaults setObject:passWordText.text forKey:@"password"];
            [defaults setObject:@"YES" forKey:@"islogin"];
            [defaults synchronize];
            NSLog(@"Login is Successfully !!");
         }
    }
    else
    {
        NSLog(@"invalid email and password");
    }
}

so,I Hope for helpful you. Thanks...

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30