- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
if([textField.textrangeOfString:@"."].location==NSNotFound)
{
isHaveDian=NO;
}
if([stringlength] >0)
{
unicharsingle = [stringcharacterAtIndex:0];//當(dāng)前輸入的字符
if((single >='0'&& single <='9') || single =='.')//數(shù)據(jù)格式正確
{
//首字母不能為0和小數(shù)點(diǎn)
if([textField.textlength] ==0)
{
if(single =='.')
{
//[self showMyMessage:@"親,第一個(gè)數(shù)字不能為小數(shù)點(diǎn)!"];
[textField.textstringByReplacingCharactersInRange:rangewithString:@""];
returnNO;
}
//if (single == '0')
//{
//
////[self showMyMessage:@"親,第一個(gè)數(shù)字不能為0!"];
//
//[textField.text stringByReplacingCharactersInRange:range withString:@""];
//
//return NO;
//
//}
}
//輸入的字符是否是小數(shù)點(diǎn)
if(single =='.')
{
if(!isHaveDian)//text中還沒有小數(shù)點(diǎn)
{
isHaveDian=YES;
returnYES;
}else{
//[self showMyMessage:@"親,您已經(jīng)輸入過小數(shù)點(diǎn)了!"];
[textField.textstringByReplacingCharactersInRange:rangewithString:@""];
returnNO;
}
}else{
if(isHaveDian) {//存在小數(shù)點(diǎn)
//判斷小數(shù)點(diǎn)的位數(shù)
NSRangeran = [textField.textrangeOfString:@"."];
if(range.location- ran.location<=2) {
returnYES;
}else{
//[self showMyMessage:@"親,您最多輸入兩位小數(shù)!"];
returnNO;
}
}else{
returnYES;
}
}
}else{//輸入的數(shù)據(jù)格式不正確
//[self showMyMessage:@"親,您輸入的格式不正確!"];
[textField.textstringByReplacingCharactersInRange:rangewithString:@""];
returnNO;
}
}
else
{
returnYES;
}
}