Html HTML5 表单输入模式货币格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5963158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML5 Form Input Pattern Currency Format
提问by user748500
Using HTML5 I have an input field that should validate against a dollar amount entered. Currently I have the following markup:
使用 HTML5 我有一个输入字段,应该根据输入的美元金额进行验证。目前我有以下标记:
<input type="number" pattern="(\d{3})([\.])(\d{2})">
This works great for an amount that is greater than 100.00 and less than 1,000.00. I am trying to write the pattern (regex) to accept different dollar amounts. Maybe upwards of 100,000.00. Is this possible?
这对于大于 100.00 且小于 1,000.00 的金额非常有效。我正在尝试编写模式(正则表达式)以接受不同的美元金额。也许超过 100,000.00。这可能吗?
采纳答案by wormhit
If you want to allow a comma delimiter which will pass the following test cases:
如果您想允许逗号分隔符,它将通过以下测试用例:
0,00 => true
0.00 => true
01,00 => true
01.00 => true
0.000 => false
0-01 => false
then use this:
然后使用这个:
^\d+(\.|\,)\d{2}$
回答by SeaCore
The best we could come up with is this:
我们能想到的最好的办法是:
^\\$?(([1-9](\\d*|\\d{0,2}(,\\d{3})*))|0)(\\.\\d{1,2})?$
^\\$?(([1-9](\\d*|\\d{0,2}(,\\d{3})*))|0)(\\.\\d{1,2})?$
I realize it might seem too much, but as far as I can test it matches anything that a human eye would accept as valid currency value and weeds out everything else.
我意识到它可能看起来太多了,但据我所知,它与人眼可以接受的任何有效货币价值相匹配,并清除了其他所有东西。
It matches these:
它匹配这些:
1 => true
1.00 => true
=> true
00 => true
0.1 => true
1,000.00 => true
,000,000 => true
5678 => true
And weeds out these:
并清除这些:
1.001 => false
02.0 => false
22,42 => false
001 => false
192.168.1.2 => false
, => false
.55 => false
2000,000 => false
回答by Toto
How about :
怎么样 :
^\d+\.\d{2}$
This matches one or more digits, a dot and 2 digits after the dot.
这匹配一位或多位数字、一个点和点后的 2 位数字。
To match also comma as thousands delimiter :
也匹配逗号作为千位分隔符:
^\d+(?:,\d{3})*\.\d{2}$
回答by user1809464
Another answer for this would be
另一个答案是
^((\d+)|(\d{1,3})(\,\d{3}|)*)(\.\d{2}|)$
This will match a string of:
这将匹配以下字符串:
- one or more numbers with out the decimal place (\d+)
- any number of commas each of which must be followed by 3 numbers and have upto 3 numbers before it (\d{1,3})(\,\d{3}|)*
- 一个或多个不带小数位的数字 (\d+)
- 任意数量的逗号,每个逗号必须后跟 3 个数字,并且前面最多有 3 个数字 (\d{1,3})(\,\d{3}|)*
Each or which can have a decimal place which must be followed by 2 numbers (.\d{2}|)
每个 or 可以有一个小数位,后面必须跟 2 个数字 (.\d{2}|)
回答by husmen73
I'm wrote this price pattern without zero price.
我写了这个没有零价格的价格模式。
(0\.((0[1-9]{1})|([1-9]{1}([0-9]{1})?)))|(([1-9]+[0-9]*)(\.([0-9]{1,2}))?)
Valid For:
适用于:
- 1.00
- 1
- 1.5
- 0.10
- 0.1
- 0.01
- 2500.00
- 1.00
- 1
- 1.5
- 0.10
- 0.1
- 0.01
- 2500.00
Invalid For:
无效:
- 0
- 0.00
- 0.0
- 2500.
- 2500.0000
- 0
- 0.00
- 0.0
- 2500。
- 2500.0000
Check my code online: http://regexr.com/3binj
在线检查我的代码:http: //regexr.com/3binj
回答by guzart
I like to give the users a bit of flexibility and trust, that they will get the format right, but I do want to enforce only digits and two decimals for currency
我喜欢给用户一些灵活性和信任,他们会得到正确的格式,但我确实想强制执行货币的数字和两位小数
^[$\-\s]*[\d\,]*?([\.]\d{0,2})?\s*$
Takes care of:
照顾:
$ 1.
-$ 1.00
$ -1.0
.1
.10
-$ 1,000,000.0
Of course it will also match:
当然它也会匹配:
$$--,92,9,29.1 => anyway after cleanup => -192,929.10
回答by Yesu Raj
Use this pattern "^\d*(\.\d{2}$)?"
使用这种模式 "^\d*(\.\d{2}$)?"