Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 656
user574089 Avatar asked Nov 28 '22 17:11

user574089


2 Answers

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];
like image 66
Praveen Avatar answered Dec 04 '22 23:12

Praveen


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];
}
like image 42
Khayrattee Wasseem Avatar answered Dec 05 '22 00:12

Khayrattee Wasseem