Does anyone know how to validate an e-mail address in Swift? I found this code:
- (BOOL) validEmail:(NSString*) emailString {
if([emailString length]==0){
return NO;
}
NSString *regExPattern = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger regExMatches = [regEx numberOfMatchesInString:emailString options:0 range:NSMakeRange(0, [emailString length])];
NSLog(@"%i", regExMatches);
if (regExMatches == 0) {
return NO;
} else {
return YES;
}
}
but I can't translate it to Swift.
I would use
NSPredicate
:for versions of Swift earlier than 3.0:
for versions of Swift earlier than 1.2:
I improved @Azik answer. I allow more special characters which are allowed by guidelines, as well as return a few extra edge cases as invalid.
The group think going on here to only allow
._%+-
in the local part is not correct per guidelines. See @Anton Gogolev answer on this question or see below:The code I use will not allow restricted out of place special characters, but will allow many more options than the majority of answers here. I would prefer more relaxed validation to error on the side of caution.
I prefer use an extension for that. Besides, this url http://emailregex.com can help you to test if regex is correct. In fact, the site offers differents implementations for some programming languages. I share my implementation for Swift 3.
Create simple extension:
Example:
You can extend following extension to anything you need:
isValidPhoneNumber
,isValidPassword
etc...Here's the reasonable solution:
"THE REASONABLE SOLUTION"
Used and tested for years in many, many huge volume apps.
1 - it avoids the many very regex mistakes you often see in these suggestions
2 - it does not allow stupid emails such as "x@x" which are technically valid, but are completely stupid - and your support staff, etc, would instantly reject anyway. If you need (for what purpose?) a solution that allows stupid emails, use another solution.
3 - it is extremely understandable, as much as can be hoped
4 - It is KISS, reliable, and tested to destruction on commercial apps with enormous numbers of users
Explanation:
In the following description, "OC" means ordinary character: so, a letter or a digit.
__firstpart ... has to start and end with an OC. For the characters in the middle you can have a few unusual characters such as underscore, but the start and end have to be OC. (It's ok to have only one OC an that's it, for example: j@blah.com)
__serverpart ... You have sections like "blah." which repeat. (So, mail.city.fcu.edu type of thing.) The sections have to start and end with an OC, but in the middle you can also have dash "-". (If you want to allow other unusual characters in there, say underscore, just add it before the dash.) It's OK to have a section which is just one OC. (As in joe@x.com or joe@w.campus.edu) You can have up to five sections; you have to have one. Finally the TLD (.com or the like) is strictly 2 to 8 letters.
Note that you simply keep the predicate as a global (trivial in Swift), no need to build it every time. This is the first thing Apple mentions about the issue in the doco.
There are a lot of right answers here, but many of the "regex" are incomplete and it can happen that an email like: "name@domain" results a valid email, but it is not. Here the complete solution: