【问题标题】:UITextfiled validate country currency in between 1 - 100UITextfield 在 1 - 100 之间验证国家货币
【发布时间】:2018-07-17 10:29:48
【问题描述】:

如何验证 1 到 100 个货币值之间的 UITextfield,最多接受 2 个十进制值。

有效。 0.25 99.99, 1.99,100.00。 其他国家货币小数表示,也。 0,25 99,99 1,99 100,00

无效 00.25, 100.25, 0000000.00, 0.125, 9.567

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
    numberFormatter.locale = [NSLocale currentLocale];// this ensures the right separator behavior
    numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
    numberFormatter.usesGroupingSeparator = YES;
    numberFormatter.usesSignificantDigits = YES;
    numberFormatter.currencyGroupingSeparator = numberFormatter.locale.groupingSeparator;
    numberFormatter.decimalSeparator = numberFormatter.locale.decimalSeparator;

NSString *expression = @"^([0-9]+)?(\\.,([0-9]{1,2})?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
                                                                       options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                    options:0 range:NSMakeRange(0, [newString length])];

if (numberOfMatches == 0)
    return NO;

在 UITextfield 内无法按下。瑞典货币小数表示“,”不是“。”我要工作计算两个输入值一个是 int 另一个值是 float

【问题讨论】:

  • NSNumberFormatter ?
  • 其 NSNumberformatter 与货币相关!
  • @PrashantTukadiya 。感谢您的输入,我再次交叉检查我的代码与我的正则表达式的问题。 NSString *表达式 = @"^[0-9]*((\\.|,)[0-9]{0,2})?$";

标签: ios objective-c validation uitextfield nsnumberformatter


【解决方案1】:

在 UItTxtfield 委托方法中尝试此代码。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSInteger Currency = [textField.text integerValue];
    if(Currency >= 100)
    {
        return NO;
    }
    else
    {
        NSString *enteredCurrency=textField.text;
        if([enteredCurrency containsString:@"."])
        {
            NSArray *array=[enteredCurrency componentsSeparatedByString:@"."];
            if([array[1] length] >= 2 || [array[0] length] >= 2)
            {
                return NO;
            }
            else
            {
                return YES;
            }
        }
        else
        {
            return YES;
        }
    }
}     

【讨论】:

    【解决方案2】:

    不建议由 yourlsef 实现此功能,因为它不可扩展。您需要使用 NumberFormatter(或 NSNumberFormatter 用于 ObjC)

        let formatter = NumberFormatter()
        formatter.numberStyle = NumberFormatter.Style.currency
        formatter.allowsFloats = false
        formatter.maximumFractionDigits = 2  //you get to control how to present the price.
        formatter.locale = locale //This should be the country Locale you need. for example: "en-us"
    

    请注意,NSLocale 还允许您访问分组分隔符和小数分隔符,因此您可以稍后查看。

    formatter.currencyGroupingSeparator = locale.groupingSeparator formatter.decimalSeparator = locale.decimalSeparator

    【讨论】:

    • 我不太明白你更新的问题。你能澄清一下吗?
    • 解决后发现问题与正则表达式有关!
    • 为了将来参考,请通过添加正确的正则表达式来编辑您的问题。
    猜你喜欢
    • 2010-09-19
    • 2023-04-04
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    相关资源
    最近更新 更多