ios - 用于货币验证的 Objective C 正则表达式

标签 ios objective-c regex uitextfield

我有一个金额输入字段,应允许用户在其中输入最小值 Rs.10 和最大值 Rs.99999,即输入字符串的长度范围在 2 到 5 之间。我希望有一个检查用户是否输入了无效数字 e.q 01,002,9999999,030。这些都是无效的,不应该接受。

这是我的示例代码:

if ([amount.text length]>=2 && [amount.text length]<=5) {
}
else {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"error" message:@"Plz enter the amout Min 10Rs" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    return;
}

最佳答案

或者,您可以使用 ^[1-9][0-9]{1,4}$ 检查此值正则表达式:

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[1-9][0-9]{1,4}$" options:NSRegularExpressionCaseInsensitive error:&error];
NSRange textRange = NSMakeRange(0, amount.text.length);
NSRange matchRange = [regex rangeOfFirstMatchInString:amount.text options:NSMatchingReportProgress range:textRange];
// Did we find a match?
if (matchRange.location != NSNotFound)
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"error" message:@"Plz enter the amout Min 10Rs" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    return;
 }

关于ios - 用于货币验证的 Objective C 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29889918/

相关文章:

ios - 如何将 Cocoapods 与 Swift 项目集成?

如果存在某个单词,但不存在另一个部分单词,则正则表达式排除匹配

使用 Matlab 的 html 标签的正则表达式

r - Tidyr 与可选组分开

html - 如何为 HTML 应用程序发出通知

ios - UITableViewController 静态单元格在推送到 navigationController 时不出现

ios - double iOS 的 NSArray( objective-c )

ios - 在 IOS 中使用 SAOP API 获取 Magento 的 session ID

iphone - 淡入/淡出整个 View ,一个 subview 除外

iphone - 在 plist 中获取图像路径