objective-c - 验证文本字段中的数字(0-9)

标签 objective-c ios validation uitextfield

我正在尝试仅验证数字的文本字段。

我正在使用以下方法:

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/

相关文章:

objective-c - 在 ARC 下返回 NSString 时如何避免临时对象

ios - 将UitableViewCell分成两种颜色

php - Joomla 3自定义服务器端表单验证规则

ios - CLLocationManager坐标

html - 如何在我的 iOS 应用程序中打开 disqus 的评论

iphone - 如何控制应用程序 bundle 中包含哪些文件?

android - iOS Storyboard 有没有像 Android 一样的布局资源文件?

validation - 验证 ui :repeat 中项目的顺序

javascript - 搜索验证正则表达式在 JavaScript 中不起作用

ios - CoreBluetooth 后台执行 10 秒?