在uitextfield中validation电话号码

我正在使用下面的代码来显示以下格式的文本字段中的电话号码

格式是123-456-7890

代码工作正常

代码如下

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *numSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789-"]; NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; int charCount = [newString length]; if ([string isEqualToString:@""]) { return YES; } if ([newString rangeOfCharacterFromSet:[numSet invertedSet]].location != NSNotFound|| [string rangeOfString:@"-"].location != NSNotFound|| charCount > 12) { return NO; } if (charCount == 3 || charCount == 7) { newString = [newString stringByAppendingString:@"-"]; } amountField.text = newString; return NO; } 

我正在使用UItextfield委托方法。

但是,当我正在编辑文本字段高达“ – ”,这意味着(如果我试图改变在123-456-7890文本字段中的数字是不validation的意思123-456如果我再次从这里input剩余的数字它显示为123-4567890

任何人都可以帮助我如何validation这个事情 在这里输入图像说明

你有一个关于validation美国和国际电话号码的好教程

http://blog.stevenlevithan.com/archives/validate-phone-number

这是我过去的做法。 这绝不是有效的,但可以完成工作,仅适用于美国数字。

 - (NSString *) getLongPhoneNumber { NSString *storedNumber = @"15125551212" /* ^^ some incoming phone number */ if(storedNumber == nil){ storedNumber = @"5125551212"; } //NSLocale *locale = [NSLocale currentLocale]; //NSString *localeString = [locale localeIdentifier]; NSString *tempStr = nil; NSRange range; range.length = 3; range.location = 3; NSString *areaCode = [storedNumber substringToIndex:3]; NSString *phone1 = [storedNumber substringWithRange:range]; NSString *phone2 = [storedNumber substringFromIndex:6]; tempStr = [NSString stringWithFormat:@"1 (%@) %@-%@", areaCode, phone1, phone2]; return tempStr; } 

这个代码使用容易检查111-111-1111格式的电话号码

  NSString *phoneRegEx = @"[0-9]{3}+-[0-9]{3}+-[0-9]{4}"; NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegEx]; if([phoneTest evaluateWithObject:phone]!=YES){ //your code } else { //your code } 

看看这个代码,这可能会有所帮助

txtlpmobile.text是string(移动不,你会进入)

  int length = [self getLength:txtLpMobile.text]; if(length == 10) { if(range.length == 0) return NO; } if(length == 3){ NSString *num = [self formatNumber:txtLpMobile.text]; txtLpMobile.text = [NSString stringWithFormat:@"(%@) ",num]; if(range.length > 0) { txtLpMobile.text = [NSString stringWithFormat:@"%@",[num substringToIndex:3]]; } } else if(length == 6) { NSString *num = [self formatNumber:txtLpMobile.text]; txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@-",[num substringToIndex:3],[num substringFromIndex:3]]; if(range.length > 0) { txtLpMobile.text = [NSString stringWithFormat:@"(%@) %@",[num substringToIndex:3],[num substringFromIndex:3]]; } } NSUInteger newLength; newLength = [txtLpMobile.text length] + [string length] - range.length; NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; return (([string isEqualToString:filtered])&&(newLength <= CHARACTER_LIMIT)); 

用于格式化数字

 -(NSString*)formatNumber:(NSString*)mobileNumber { mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""]; int length = [mobileNumber length]; if(length > 10) { mobileNumber = [mobileNumber substringFromIndex: length-10]; } return mobileNumber; } 

为了获得长度

 -(int)getLength:(NSString*)mobileNumber { mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""]; mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""]; int length = [mobileNumber length]; return length; }