引入协议
<UITextFieldDelegate>
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//如果是限制只能输入数字的文本框
if (_moneyTextField==textField) {
return [self validateNumber:string];
}
//否则返回yes,不限制其他textfield
return YES;
}
- (BOOL)validateNumber:(NSString*)number {
BOOL res = YES;
NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
int i = 0;
while (i < number.length) {
NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
NSRange range = [string rangeOfCharacterFromSet:tmpSet];
if (range.length == 0) {
res = NO;
break;
}
i++;
}
return res;
}
//限制弹出数字键盘
_IDField.keyboardType = UIKeyboardTypeNumberPad;
//修改return按键样式
_IDField.returnKeyType = UIReturnKeySearch;//变为搜索按钮
//新版手机正则表达式(手机号换的太快,这里就做个简单的判断)
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
if (mobileNum.length != 11)
{
return NO;
}
return YES;
}