我使用以下代码以下列格式在文本字段中显示电话号码
,格式为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中的数字,则表示如果我再次输入剩余数字,则无法验证该值是否为123-456从这里它显示为123-4567890
任何人都可以帮我验证这件事
答案 0 :(得分:1)
您有一个关于验证美国和国际电话号码的好教程
http://blog.stevenlevithan.com/archives/validate-phone-number
答案 1 :(得分:0)
这是我以前做过的事情。这绝不是有效的,但可以完成工作,仅适用于美国数字。
- (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;
}
答案 2 :(得分:0)
此代码使用方便检查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
}
答案 3 :(得分:0)
看看这段代码,这可能对你有所帮助
txtlpmobile.text是字符串(手机不输入)
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;
}