- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
if (textField == _dosageTextField) {
if([textField.textrangeOfString:@"."].location== NSNotFound) {
isHaveDian = NO;
}
if([string length] > 0) {
unichar single = [stringcharacterAtIndex:0];//当前输入的字符
if ((single >= '0' && single <= '9') || single == '.') {//数据格式正确
//首字母不能为0和小数点
if([textField.textlength] == 0){
if(single == '.') {
[selfshowError:@"亲,第一个数字不能为小数点"];
[textField.textstringByReplacingCharactersInRange:rangewithString:@""];
returnNO;
}
if (single == '0') {
[selfshowError:@"亲,第一个数字不能为0"];
[textField.textstringByReplacingCharactersInRange:rangewithString:@""];
returnNO;
}
}
//输入的字符是否是小数点
if (single == '.') {
if(!isHaveDian)//text中还没有小数点
{
isHaveDian = YES;
returnYES;
}else{
[selfshowError:@"亲,您已经输入过小数点了"];
[textField.textstringByReplacingCharactersInRange:rangewithString:@""];
returnNO;
}
}else{
if (isHaveDian) {//存在小数点
//判断小数点的位数
NSRange ran = [textField.textrangeOfString:@"."];
if (range.location- ran.location<= 10) {
returnYES;
}else{
[selfshowError:@"亲,您最多输入两位小数"];
returnNO;
}
}else{
returnYES;
}
}
}else{//输入的数据格式不正确
[selfshowError:@"亲,您输入的格式不正确"];
[textField.textstringByReplacingCharactersInRange:rangewithString:@""];
returnNO;
}
}
else
{
returnYES;
}
}
returnYES;
}
- (void)showError:(NSString*)errorString {
[selfrsva_presentViewControllerWithMessage:errorString];
}
- (void)rsva_presentViewControllerWithMessage:(NSString*)message {
UIAlertController*alertController = [RSMethodylg_presentAlertControllerWithTitle:@"提示"message:message];
[selfpresentViewController:alertControlleranimated:YEScompletion:nil];
}