Friday, August 24, 2012

E-Mail address validation in iOS app

Email validation custom method :

 -(BOOL) validEmailAddress:(NSString*) emailString {
    NSString *regExPattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-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])];
    if (regExMatches == 0)
        return NO; // email invalid
     else
        return YES; // email valid
  }

Now, just call above method from where you want to validate email address (pass your email address as a emailString to this method).
For eg:
 if ([self validEmailAddress:yourEmailAddressText] == YES) {
      //email valid
}
else {
    //email invalid
}

5 comments:

  1. I need to validate email id in a way that it should accept only gmail and gtalk ids and other than that i need to show warning

    ReplyDelete
  2. ....@test.com check this email

    ReplyDelete
  3. Tired of hearing about how much we love remote working? Take it from some others! Rounded up below are 27 Quotes on Remote Work and flexible lifestyles from some major power players.

    ReplyDelete
  4. 700 pages printed manual available for comprehensive courses
    24 x 7 access from anywhere for online courses
    Experts in HIPAA Privacy Officer Training and HIPAA Security Officer Training with more than 18+ years experience

    ReplyDelete
  5. The information shared about ios app development is of top quality which has to get appreciated at all levels.

    ReplyDelete