首先需要输入数字时限制其弹出的键盘必定为数字键盘
TextField.keyboardType = UIKeyboardTypePhonePad
其次判断输入的长度,如果用户输入的长度大于11位时只截取前11位
if (TextField.text.length >= 11) {
TextField.text = [TextField.text substringToIndex:11];
}
+ (BOOL)isPhoneNumber:(NSString*)strNum
{
NSMutableArray *arrayNum = [[NSMutableArray alloc] initWithObjects:@"134",@"135",@"136",@"137",@"138",@"139",@"150",@"151",@"152",@"153",@"154",@"155",@"156",@"157",@"158",@"159",@"182",@"187",@"188",@"147",@"130",@"131",@"132",@"185",@"186",@"133",@"180",@"181",@"189", nil];
NSString *astring = [strNum substringToIndex:3];
for (int i = 0; i<arrayNum.count; i++) {
if ([arrayNum[i] isEqualToString:astring]) {
return TRUE;
}
}
return FALSE;
}