Email validation on textField in iOS

前端 未结 14 571

In iOS App, how to add Email validation on UITextField?

14条回答
  •  旧巷少年郎
    2020-11-30 18:12

    --it's easy to validate your email id by calling validateEmail method:

    -(BOOL)validateEmail:(NSString *)email      
    {
        NSString *emailRegex = @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";  
    
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    
        return [emailTest evaluateWithObject:email];
    }
    

    Verify your email id here....

    BOOL eb=[self validateEmail:**youremailtextfield**];
    
    if(!eb)
    {
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Please enter correct email id"
                                                              delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    
        [alertsuccess show];
        [alertsuccess release]; 
    }
    

提交回复
热议问题