嗨我的最小值为100,最大值为400.用户应输入此值。如果他们输入400.5就不应该接受。这是我的文本域的验证。我使用了以下代码
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *textFieldNumber = [formatter numberFromString:[dynamicHealthyHeartTxtFld text]];
NSNumber *MaxNumber = [formatter numberFromString:[[healthyHeartFieldsAry objectAtIndex:getHHTag] objectForKey:@"MaxVal"]];
NSNumber *MinNumber = [formatter numberFromString:[[healthyHeartFieldsAry objectAtIndex:getHHTag] objectForKey:@"MinVal"]];
if ([self checkForDecimalValue:dynamicHealthyHeartTxtFld.text])
{
if (([textFieldNumber decimalValue] < [MinNumber decimalValue]) || ([textFieldNumber decimalValue] > [MaxNumber decimalValue]) ) {
if ([textFieldNumber decimalValue] < [MinNumber decimalValue]) {
Custom_Alert *alert=[[Custom_Alert alloc] initWithTitle:@"Alert" message:[NSString stringWithFormat:@"The Value you entered in %@ is outside the acceptable range.Please correct and Continue ",[[healthyHeartFieldsAry objectAtIndex:getHHTag] objectForKey:@"TestName"]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
enterAlert=YES;
[alert release];
}
else
{
Custom_Alert *alert=[[Custom_Alert alloc] initWithTitle:@"Alert" message:[NSString stringWithFormat:@"The Value you entered is %@ outside the acceptable range.Please correct and Continue",[[healthyHeartFieldsAry objectAtIndex:getHHTag] objectForKey:@"TestName"]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
enterAlert=YES;
[alert release];
}
}
}
如果我使用此代码。它接受400.5。请帮我解决这个问题。
答案 0 :(得分:2)
您需要将文字字段返回的NSString
转换为NSNumber
:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *textFieldNumber = [formatter numberFromString:[textField text]];
然后使用
[textFieldNumber decimalValue] > 100.0 && [textFieldNumber decimalValue] < 400
检查范围。
答案 1 :(得分:0)
我已将(textfield.text)字符串转换为double值,并且我将double值与double值进行比较。它工作正常。谢谢你的所有答案。