简体   繁体   English

使用正则表达式验证掩码和限制货币范围

[英]Using regex to validate mask and limit range in currency

I am working on a feature on my application and now have a requirement that need a regex to validate the mask typed by the user (it is ok) and limit the range typed.我正在开发我的应用程序的一项功能,现在需要一个正则表达式来验证用户输入的掩码(没关系)并限制输入的范围。 For example, if I type:例如,如果我输入:

100,00 - can pass
50,00 - can pass
100.000,00 - can pass
100.000,01 - more than 100k not pass

Now, I have my regex working to validate the mask, but I can not limit the range...现在,我有我的正则表达式来验证掩码,但我不能限制范围......

^\$?(([1-9]\d{0,2}(.\d{3})*)|0)?\,\d{1,2}$

Anyone knows how can I achieve this limitation?任何人都知道我怎样才能实现这个限制?

You need to validate the 100.000,00 part separately.您需要单独验证100.000,00部分。 Also, remember that \.另外,请记住\. matches a dot, and that in your current regex, you allow an unlimited number of repetitions, so 100.000.000.000,00 currently passes.匹配一个点,并且在您当前的正则表达式中,您允许无限次数的重复,因此100.000.000.000,00当前通过。

Try this:尝试这个:

^\$?(?:100\.000,00|(?:[1-9]\d?\.\d{3}|[1-9]\d{0,2}|0)\,\d{1,2})$

Test it live on regex101.com .在 regex101.com 上进行实时测试。

Explanation:解释:

^                   # Start of string
\$?                 # Match optional dollar sign
(?:                 # Group: Match either
 100\.000,00        # 100.000,00
|                   # or
 (?:                # Group: Match either
  [1-9]\d?\.\d{3}   # a number between 1.000 and 99.999
 |                  # or
  [1-9]\d{0,2}      # a number between 1 and 999  
 |                  # or
  0                 # 0
 )                  # End of inner group
 \,\d{1,2}          # Match , and one or two digits
)                   # end of outer group
$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM