user574089
user574089

Reputation: 350

Email Validation in iOS

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.

Upvotes: 4

Views: 17188

Answers (7)

Mr. Pravin Kantariya
Mr. Pravin Kantariya

Reputation: 31

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...

Upvotes: -2

Patel Jigar
Patel Jigar

Reputation: 2151

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];
}

Upvotes: 0

benzguo
benzguo

Reputation: 176

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

Upvotes: 2

Pushkraj Lanjekar
Pushkraj Lanjekar

Reputation: 2294

- (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];
    }

Upvotes: 3

Wasseem Khayrattee
Wasseem Khayrattee

Reputation: 682

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];
}

Upvotes: 8

petert
petert

Reputation: 6692

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.

Upvotes: 1

Praveen
Praveen

Reputation: 301

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];

Upvotes: 11

Related Questions