validation目标c中相同文本字段中的电话号码和电子邮件

任何一个帮助解决这个问题我都是新的发展

我需要login电话号码以及电子邮件ID与相同的文本字段我怎么能达到这一点,因此我必须设置电话号码以及美国像(838) – (838)-3333格式电话号码的10位数限制。 。和电子邮件ID相同的文本字段..如何validation用户input电话号码或电子邮件..任何人帮我解决这个问题在这里感谢这里是我的代码

if (!([self.usernameTextField.text validEmail] || [self.usernameTextField.text validateMobile])){ [self ShowAlert:@"Please enter a valid email address/phonenumber"]; } - (BOOL)validEmail { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; BOOL isValid = [emailTest evaluateWithObject:self]; return isValid; } - (BOOL)validateMobile { NSString *ACCEPTABLE_CHARECTERS = @" 0123456789+-"; NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARECTERS] invertedSet]; NSString *filtered = [[self componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; // NSString* formattedNumber = [Contact formatPhoneNumber:phoneNumber codeLength:3 segmentLength:4]; return [self isEqualToString:filtered]; } 

在这里我添加我的答案,并根据您的需要定制自己。

步骤1

连接文本字段并调用当前类的委托,例如

  @interface ViewController ()<UITextFieldDelegate> @property (strong, nonatomic) IBOutlet UITextField *currentTextfield; @end 

第2步

获取文本字段委托shouldChangeCharactersInRange的第一个字符,并检查是否是号码(电话号码)或string(电子邮件)

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)textEntered { NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: textEntered]; NSString *abnRegex = @"[A-Za-z]+"; NSPredicate *abnTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", abnRegex]; if (([abnTest evaluateWithObject:resultingString] || resultingString.length == 0)) { NSLog(@"its email"); } else { NSLog(@"its phone number"); } return YES; } 

第3步

如果是电话号码, (838)-(838)-3333格式排列字母

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)textEntered { NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: textEntered]; NSString *abnRegex = @"[A-Za-z]+"; NSPredicate *abnTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", abnRegex]; if (([abnTest evaluateWithObject:resultingString] || resultingString.length == 0)) { NSLog(@"its email"); } else { NSLog(@"its phone number"); //(838)-(838)-3333 if (range.length == 1) { // Delete button was hit.. so tell the method to delete the last char. textField.text = [self formatPhoneNumber:resultingString deleteLastChar:YES]; } else { textField.text = [self formatPhoneNumber:resultingString deleteLastChar:NO ]; } return false; } return YES; } -(NSString*) formatPhoneNumber:(NSString*) simpleNumber deleteLastChar:(BOOL)deleteLastChar { if(simpleNumber.length==0) return @""; // use regex to remove non-digits(including spaces) so we are left with just the numbers NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\\s-\\(\\)]" options:NSRegularExpressionCaseInsensitive error:&error]; simpleNumber = [regex stringByReplacingMatchesInString:simpleNumber options:0 range:NSMakeRange(0, [simpleNumber length]) withTemplate:@""]; // check if the number is to long if(simpleNumber.length>10) { // remove last extra chars. simpleNumber = [simpleNumber substringToIndex:10]; } if(deleteLastChar) { // should we delete the last digit? simpleNumber = [simpleNumber substringToIndex:[simpleNumber length] - 1]; } // 123 456 7890 // format the number.. if it's less then 7 digits.. then use this regex. if(simpleNumber.length<7) simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\\d{3})(\\d+)" withString:@"($1)-($2)" options:NSRegularExpressionSearch range:NSMakeRange(0, [simpleNumber length])]; else // else do this one.. simpleNumber = [simpleNumber stringByReplacingOccurrencesOfString:@"(\\d{3})(\\d{3})(\\d+)" withString:@"($1)-($2)-$3" options:NSRegularExpressionSearch range:NSMakeRange(0, [simpleNumber length])]; return simpleNumber; } 

步骤4

最后validation您的电子邮件或电话号码button的行动,如果是有效的或没有。

 NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx]; NSString *phoneRegex = @" (((\d{3}) ?)-((\d{3}) ?))?-\d{4}"; NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex]; if([emailTest evaluateWithObject:currentTextfield.text] == NO && [phoneTest evaluateWithObject:currentTextfield.text] == NO) { NSLog(@"Please enter a valid email address/phonenumber"); } else { //login successful }