簡體   English   中英

使用正則表達式驗證掩碼和限制貨幣范圍

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

我正在開發我的應用程序的一項功能,現在需要一個正則表達式來驗證用戶輸入的掩碼(沒關系)並限制輸入的范圍。 例如,如果我輸入:

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

現在,我有我的正則表達式來驗證掩碼,但我不能限制范圍......

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

任何人都知道我怎樣才能實現這個限制?

您需要單獨驗證100.000,00部分。 另外,請記住\. 匹配一個點,並且在您當前的正則表達式中,您允許無限次數的重復,因此100.000.000.000,00當前通過。

嘗試這個:

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

在 regex101.com 上進行實時測試。

解釋:

^                   # 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