我正在尝试仅验证数字的文本字段。
我正在使用以下方法:
if(isnumber(noOfPassengers.text.intValue))
{
NSLog(@"value entered correctly.");
return ;
}
else
NSLog(@"Error: Only numerica values are accepted");
isnumber()函数无法正常工作。我只想验证数字。卡在这里。还有其他内置功能可以检查数字甚至字符吗?
最佳答案
使用NSScanner:
int iValue;
if (noOfPassengers.text.length > 0 && [[NSScanner scannerWithString:noOfPassengers.text] scanInt:&iValue]) {
//do smomething with iValue (int value from noOfPassengers.text)
NSLog(@"value entered correctly.");
return ;
}
else
NSLog(@"Error: Only numerica values are accepted");
关于objective-c - 验证文本字段中的数字(0-9),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8825363/