Create a function for validating email like the code below :
- (BOOL)validateEmail:(NSString *)emailStr
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:emailStr];
}
UITextField delegate has a method textFieldDidEndEditing, write the code below to display an alert message when email is not valid :
if (textField == email)
{
if(![self validateEmail:[email text]])
{
// user entered invalid email address
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Enter a valid email address." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
//email.text=@"";
}
}
No comments:
Post a Comment