How to Implement valid Email behavior code for iPhone apps

Source: Internet
Author: User

IPhone DevelopmentAbout applicationsValid Email actionsThe code implementation case is the content to be introduced in this article. It is mainly used to learn the judgment of valid mailbox behaviors. This article uses two methods to solve this problem.Valid Email actionsFor more information, see this article.

1. code used to determine valid mailbox Behaviors

If your App requires a user to register by email, how can you determine that the user has entered the correct email address? For example, if you enter a long string without "@", or the string contains characters not allowed in the email address. The favormm blog shares code to determine whether the email address is valid, hoping to help developers.

 
 
  1. BOOL NSStringIsValidEmail(NSString *checkString)    
  2. {    
  3.     NString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";    
  4.     NSString *laxString = @".+@.+\.[A-Za-z]{2}[A-Za-z]*";    
  5.     NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;    
  6.     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];    
  7.     return [emailTest evaluateWithObject:checkString];    
  8. }  

2. Another group of codes used to determine valid mailbox Behaviors

The above describes the code used to determine the valid behaviors of a mailbox. Next we will introduce another method to check the legitimacy of a mailbox through string operations. The Code is as follows:

 

  1. -(BOOL)validateEmail:(NSString*)email{  
  2.      
  3.     if( (0 != [email rangeOfString:@"@"].length) &&  (0 != [email rangeOfString:@"."].length) )  
  4.     {  
  5.         NSMutableCharacterSet *invalidCharSet = [[[[NSCharacterSet alphanumericCharacterSet] invertedSet]mutableCopy]autorelease];  
  6.         [invalidCharSet removeCharactersInString:@"_-"];  
  7.          
  8.         NSRange range1 = [email rangeOfString:@"@" options:NSCaseInsensitiveSearch];  
  9.          
  10.         // If username part contains any character other than "."  "_" "-"  
  11.          
  12.         NSString *usernamePart = [email substringToIndex:range1.location];  
  13.         NSArray *stringsArray1 = [usernamePart componentsSeparatedByString:@"."];  
  14.         for (NSString *string in stringsArray1) {  
  15.             NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet: invalidCharSet];  
  16.             if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])  
  17.                 return NO;  
  18.         }  
  19.          
  20.         NSString *domainPart = [email substringFromIndex:range1.location+1];  
  21.         NSArray *stringsArray2 = [domainPart componentsSeparatedByString:@"."];  
  22.          
  23.         for (NSString *string in stringsArray2) {  
  24.             NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:invalidCharSet];  
  25.             if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])  
  26.                 return NO;  
  27.         }  
  28.          
  29.         return YES;  
  30.     }  
  31.     else // no ''@'' or ''.'' present  
  32.         return NO;  

Summary:IPhone DevelopmentAbout applicationsValid Email actionsThe content of the code implementation case has been introduced. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.