Email Validation in iOS
Solution 1
I think it largely depends on the minimum version of iOS an App will support, and if there's a built-in class that'll do the job, use it. As you hinted, you'd use some other 3rd-party framework to support older versions in a consistent way.
Right now, I'd use NSPredicate
or NSRegularExpression
, both are supported from iOS 4 onwards, which is quite likely to be the minimum supported version for new iOS Apps, if not iOS 5.
Useful post.
Solution 2
i am using NSPredicate always...and it is working fine
NSString *emailid = emailField.text;
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest =[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL myStringMatchesRegEx=[emailTest evaluateWithObject:emailid];
Solution 3
My answer is a refactored one from the excellent solution provided in this link..
///Returns YES (true) if EMail is valid
+(BOOL) IsValidEmail:(NSString *)emailString Strict:(BOOL)strictFilter
{
NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = strictFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:emailString];
}
Solution 4
- (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];
}
On Button Click :
if (![self validateEmail:[txtEmail text]])
{
alertValidator.message = @"Please Enter Valid Email Address !";
[alertValidator show];
txtEmail.text = @"";
[txtEmail becomeFirstResponder];
}
Solution 5
I think the best option, hands down, is Mailgun's free email validation API. I've written a simple objective-C wrapper for it:
https://github.com/benzguo/BZGMailgunEmailValidation
BZGMailgunEmailValidator *validator =
[BZGMailgunEmailValidator validatorWithPublicKey:YOUR_PUBLIC_KEY];
[validator validateEmailAddress:self.emailFieldCell.textField.text
success:^(BOOL isValid, NSString *didYouMean) {
// Validation succeeded
} failure:^(NSError *error) {
// Validation failed
}];
If there's a connection error, the validator falls back to regex-based validation.
If you still want to validate emails using a regular expression, check out:
user574089
Updated on June 26, 2022Comments
-
user574089 almost 2 years
Email validation checking in iPhone programming can be done using RegexKitLite library (iOS2.0),
NSPredicate
(iOS 3.0 onwards) andNSRegularExpression
(iOS 4.0). But can anybody state what is the advantage of one over the other and which is the best validating option of the three stated. -
user574089 over 12 yearsThank you for giving solution particular to the question only. Right now i am working on iOS 5.0 and NSPredicate is working fine on it.
-
venki about 10 yearshow to restrict the user to enter his email id as only @gmail.com in text field.