Possible Duplicate:
Ensure User has entered email address string in correct format?
I have UITextField
in which I take the email address from user that they enter, and I want to validate that email address, such as I would it should check that it contains symbols like @ sign and other email characters.
If there is an error in the email address then it should show a UIAlertView
that would say "enter a valid email address".
Objective C Style
NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,10}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
if ([emailTest evaluateWithObject:email.text] == NO) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test!" message:@"Please Enter Valid Email Address." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
Swift Style
class func isValidEmail(emailString:String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,10}"
var emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
let result = emailTest?.evaluateWithObject(emailString)
return result!
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With