Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to check if the email is valid in iOS sdk

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.17k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: