NSString *emailString = textField.text; **// storing the entered email in a string.**
**// Regular expression to checl the email format.**
NSString *emailReg = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailReg];
if (([emailTest evaluateWithObject:emailString] != YES) || [emailStringisEqualToString:@""])
{
UIAlertView *loginalert = [[UIAlertView alloc] initWithTitle:@" Enter Email in" message:@"abc@example.com format" delegate:self
cancelButtonTitle:@"OK" otherButtonTitles:nil];
enter code here
[loginalert show];
[loginalert release];
}
If email is invalid, it will remind the user with an alert box.
Hope this might be helpful for you all.
- (BOOL)isValidEmail {
// Trim whitespace first
NSString *trimmedText = [self stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
if (self && self.length > 0) return NO;
NSError *error = nil;
NSDataDetector *dataDetector = [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:&error];
if (!dataDetector) return NO;
// This string is a valid email only if iOS detects a mailto link out of the full string
NSArray<NSTextCheckingResult *> *allMatches = [dataDetector matchesInString:trimmedText options:kNilOptions range:NSMakeRange(0, trimmedText.length)];
if (error) return NO;
return (allMatches.count == 1 && [[[allMatches.firstObject URL] absoluteString] isEqual:[NSString stringWithFormat:@"mailto:%@", self]]);
}