- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
if([textField.textrangeOfString:@"."].location==NSNotFound)
{
isHaveDian=NO;
}
if([stringlength] >0)
{
unicharsingle = [stringcharacterAtIndex:0];//当前输入的字符
if((single >='0'&& single <='9') || single =='.')//数据格式正确
{
//首字母不能为0和小数点
if([textField.textlength] ==0)
{
if(single =='.')
{
//[self showMyMessage:@"亲,第一个数字不能为小数点!"];
[textField.textstringByReplacingCharactersInRange:rangewithString:@""];
returnNO;
}
//if (single == '0')
//{
//
////[self showMyMessage:@"亲,第一个数字不能为0!"];
//
//[textField.text stringByReplacingCharactersInRange:range withString:@""];
//
//return NO;
//
//}
}
//输入的字符是否是小数点
if(single =='.')
{
if(!isHaveDian)//text中还没有小数点
{
isHaveDian=YES;
returnYES;
}else{
//[self showMyMessage:@"亲,您已经输入过小数点了!"];
[textField.textstringByReplacingCharactersInRange:rangewithString:@""];
returnNO;
}
}else{
if(isHaveDian) {//存在小数点
//判断小数点的位数
NSRangeran = [textField.textrangeOfString:@"."];
if(range.location- ran.location<=2) {
returnYES;
}else{
//[self showMyMessage:@"亲,您最多输入两位小数!"];
returnNO;
}
}else{
returnYES;
}
}
}else{//输入的数据格式不正确
//[self showMyMessage:@"亲,您输入的格式不正确!"];
[textField.textstringByReplacingCharactersInRange:rangewithString:@""];
returnNO;
}
}
else
{
returnYES;
}
}
网友评论