Double opt-in is the best way to validate email addresses. If your customers sign up for your email list or newsletter, send them an email that requests them to validate by responding. Many people don't like this option, as it may reduce your overall opt-in rates, but we believe it is better to have good data.
The answer to Using a regular expression to validate an email address explains in great detail that the grammar specified in RFC 5322 is too complicated for primitive regular expressions.
I recommend a real parser approach like MKEmailAddress.
As quick regular expressions solution see this modification of DHValidation:
- (BOOL) validateEmail: (NSString *) candidate {
NSString *emailRegex =
@"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
@"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
@"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
@"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
@"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
@"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
@"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", emailRegex];
return [emailTest evaluateWithObject:candidate];
}
Read the RFC. Almost everyone that thinks they know how to parse/clean/validate an email address is wrong.
https://www.rfc-editor.org/rfc/rfc2822 Section 3.4.1 is very useful. Notice
dtext = NO-WS-CTL / ; Non white space controls %d33-90 / ; The rest of the US-ASCII %d94-126 ; characters not including "[", ; "]", or "\"
Yes, that means +, ', etc are all legit.
The best solution I have found so far (and the one I ended up going with) is to add RegexKitLite To the project which gives access to regular expressions via NSString Categories.
It is quite painless to add to the project and once in place, any of the regular expression email validation logic will work.
A good start is to decide what do you and do you not want to accept as an email address?
99% of of email addresses look like this: [email protected] or [email protected]
However, it's technically legal to have an email address like this: f!#$%&'*+-/=?^_
{|}~"ha!"@com
There are probably only a handful of valid emails in the world for top-level domains, and almost nobody uses most of those other characters (especially quotes and backticks), so you might want to assume that these are all invalid things to do. But you should do so as a conscious decision.
Beyond that, do what Paul says and try to match the input to a regular expression like this: ^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,}$
That one will match pretty much everybody's email address.
While the focus on regular expressions is good, but this is only a first and necessary step. There are other steps that also need to be accounted for a good validation strategy.
Two things on top of my head are :
DNS validation to make sure the domain actually exists.
After dns validation, you can also choose to do an smtp validation. send a call to the smtp server to see if the user actually exists.
In this way you can catch all kinds of user errors and make sure it is a valid email.
This function is simple and yet checks email address more thoroughly. For example, according to RFC2822 an email address must not contain two periods in a row, such as [email protected]
It is also important to use anchors in regular expressions as seen in this function. Without anchors the following email address is considered valid: first;name)[email protected](blah because the [email protected] section is valid, ignoring first;name) at the beginning and (blah at the end. Anchors force the regular expressions engine to validate the entire email.
This function uses NSPredicate which does not exist in iOS 2. Unfortunately it may not help the asker, but hopefully will help others with newer versions of iOS. The regular expressions in this function can still be applied to RegExKitLite in iOS 2 though. And for those using iOS 4 or later, these regular expressions can be implemented with NSRegularExpression.
- (BOOL)isValidEmail:(NSString *)email
{
NSString *regex1 = @"\\A[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,4}\\z";
NSString *regex2 = @"^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*";
NSPredicate *test1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];
NSPredicate *test2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex2];
return [test1 evaluateWithObject:email] && [test2 evaluateWithObject:email];
}
See validate email address using regular expression in Objective-C.
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