Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email Validation iPhone SDK

Assuming I have created IBOutlet UITextField *emailValidate;

And the empty method

-(IBAction)checkEmail:(id)sender {

// Add email validation code here.

}

And linked the File Owner file to the TextField, what code would I have to insert in the method to validate an email adress? checking that only one '@' is included, and only one '.' is included?

like image 446
Anthony Avatar asked Dec 01 '22 03:12

Anthony


1 Answers

Use the function below...

+(BOOL) validateEmail: (NSString *) email 
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL isValid = [emailTest evaluateWithObject:email];
return isValid;
}
like image 183
Suresh Varma Avatar answered Dec 04 '22 11:12

Suresh Varma