Hi readers,
Many apps require email validation in iOS. Here the function that can be used to check if email string is valid or not. It can be achieved by NSPredicate like following:
-(BOOL)isValidEmail:(NSString *)checkString
{
long numberOfAtPieces = [[checkString componentsSeparatedByString:@"@"] count];
if (numberOfAtPieces > 2){
return NO;
}
BOOL stricterFilter = NO;
NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
NSString *laxString = @".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:checkString];
}
Just past the checkString as email you want to validate and it returns 'YES' if email is valid else returns 'NO'.
0 Comment(s)