javascript 用于验证 PRICE 的 RegExp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15958808/
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
RegExp for validating PRICE
提问by user1022521
I am trying to validate a Price field:
我正在尝试验证价格字段:
Should not allow:
不应该允许:
- white spaces
- alphabets
- negative values
- 空白
- 字母表
- 负值
Should allow:
应该允许:
- numbers
- commas
- decimals
- 数字
- 逗号
- 小数点
回答by CS?
RegExp for validating Price Fields:
用于验证价格字段的 RegExp:
/^(\d*([.,](?=\d{3}))?\d+)+((?!)[.,]\d\d)?$/
Explained demo: http://regex101.com/r/uG5lI0/1
解释演示:http: //regex101.com/r/uG5lI0/1
Javascript code to validate:
用于验证的 Javascript 代码:
/^(\d*([.,](?=\d{3}))?\d+)+((?!)[.,]\d\d)?$/.test(input)
Will validate only if in correct format, with dots or commas in their respective places.
仅在格式正确时进行验证,在各自的位置使用点或逗号。
International format for the en_US locale & US national format:
en_US 语言环境和美国国家格式的国际格式:
- 134.56
- 1,234.56
- 2,991,234.00
- 134.56
- 1,234.56
- 2,991,234.00
Italian national format with 2 decimals:
带 2 位小数的意大利国家格式:
- 134,56
- 21.234,56
- 1.234,56
- 9.321.234,56
- 134,56
- 21.234,56
- 1.234,56
- 9.321.234,56
International format for the de_DE locale:
de_DE 语言环境的国际格式:
- 134,56
- 1234,56
- 98281234,56
- 134,56
- 1234,56
- 98281234,56
Decimals are optional, validation for whole amounts:
小数是可选的,对整数进行验证:
- 1234
- 1,234
- 2,991,234
- 1.234
- 9.321.234
- 1234
- 1,234
- 2,991,234
- 1.234
- 9.321.234
function validatePrice(input) {
return /^(\d*([.,](?=\d{3}))?\d+)+((?!)[.,]\d\d)?$/.test(input);
}
['WRONG',
'1,234,56',
'1.234.56',
'-1993',
'918,27.63',
'122.42.24',
'1,89,2',
'',
'Intl. format & US national format',
'2.56',
'14.56',
'134.56',
'1,234.56',
'2,991,234.00',
'',
'Italian national format with 2 decimals',
'9,56',
'24,56',
'134,56',
'721.234,56',
'21.234,56',
'1.234,56',
'9.321.234,56',
'69.321.234,56',
'269.321.234,56',
'1.269.321.234,56',
'International format for the de_DE locale',
'1,56',
'14,56',
'134,56',
'1234,56',
'98281234,56',
'No cents',
'1',
'14',
'134',
'1,234',
'2,991,234',
'9',
'24',
'134',
'1.234',
'9.321.234',
'1',
'14',
'134',
'1234',
'98281234'
].forEach(function(n) {
document.getElementById('results').innerHTML += "'" + n + "' => " + validatePrice(n) + "<br>";
})
<div id="results"></div>
回答by ryanbrill
This regex ought to do it:
这个正则表达式应该这样做:
^[\d\.,]+$
^[\d\.,]+$
Edit:
编辑:
Here's a better regex, to allow more sane number formats (for US dollars). Still not perfect - would need to know exactly what validation you are expecting, if this isn't quite it.
这是一个更好的正则表达式,以允许更合理的数字格式(美元)。仍然不完美 - 需要确切地知道您期望什么验证,如果这不是完全正确的话。
^(\d{1,3})?(,?\d{3})*(\.\d{2})?$
^(\d{1,3})?(,?\d{3})*(\.\d{2})?$
回答by saarp
It's easiest to just include the things you want and exclude everything else. It looks like you want digits, commas, and periods. A regex for this would look like /^[\d.,]+$/
. This doesn't specify any order for the formatting. If you wanted commas to only appear in groups of 3 digits (not great of internationalization, but functional for US) for instance, you would need something more like /^\d{,3}(,\d{3})*(\.\d+)?$/
- 0-3 digits followed by 0 or more groups of ',NNN' with an optional '.N').
最简单的方法是只包含您想要的内容并排除其他所有内容。看起来您想要数字、逗号和句点。这个的正则表达式看起来像/^[\d.,]+$/
. 这不指定格式的任何顺序。例如,如果您希望逗号只出现在 3 位数字组中(不是很好的国际化,但适用于美国),您需要更像/^\d{,3}(,\d{3})*(\.\d+)?$/
- 0-3 位数字后跟 0 组或更多组 ',NNN' 和可选的“.N”)。
If you wanted to separately detect the things you wanted to avoid, you could use something like /[-A-z \t]/
- dash, letters (upper and lower), spaces or tabs.
如果你想单独检测你想避免的东西,你可以使用诸如/[-A-z \t]/
破折号、字母(上下)、空格或制表符之类的东西。