JavaScript 中美元金额的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8829765/
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
Regular Expression for dollar amount in JavaScript
提问by SquidScareMe
I know this has been done a thousand times before but I'm having trouble with getting this right. I need to format the value of a text input for dollar amounts. No commas should be allowed. Only two decimal places should be allowed. Examples of what should be allowed:
我知道这之前已经做过一千次了,但我很难做到这一点。我需要格式化美元金额的文本输入值。不允许有逗号。只应允许两位小数。应该允许的例子:
- 100
- 100.10
- $100
- $100.25
- 100
- 100.10
- 100 美元
- 100.25 美元
Below is my current incomplete regular expression. It currently allows multiple dollar signs to be included anywhere (not just at beginning), multiple decimal points to be included anywhere, and more than two decimal places which it shouldn't.
下面是我目前不完整的正则表达式。它目前允许在任何地方包含多个美元符号(不仅仅是在开头),在任何地方包含多个小数点,以及超过两个不应该包含的小数位。
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" language="Javascript">
$(function(){
jQuery('.dollarAmountOnly').keyup(function () {
this.value = this.value.replace(/[^var r = /^$?[0-9]+(\.[0-9][0-9])?$/;
console.log(r.test("12.55")); //true
console.log(r.test(".55")); //true
console.log(r.test("")); //true
console.log(r.test(".")); //false
console.log(r.test(".2")); //false
console.log(r.test("$")); //false
console.log(r.test(".555")); //false
-9\.]/g, '');
});
});
</script>
</head>
<body>
<input type="text" class="dollarAmountOnly"/>
</body>
</html>
I would greatly appreciate help. Thanks!
我将不胜感激。谢谢!
回答by Adam Rackis
EDIT
编辑
Sorry, your question clearly asked for exactly two decimal places. This is what you want:
抱歉,您的问题明确要求精确到小数点后两位。这就是你想要的:
var r = /^$?[0-9]+\.?[0-9]?[0-9]?$/;
console.log(r.test("12.55")); //true
console.log(r.test(".55")); //true
console.log(r.test(".")); //true
console.log(r.test("")); //true
console.log(r.test("$")); //false
console.log(r.test(".555")); //false
You have to escape your dollar sign, then you want two option digits after the decimal point:
您必须转义美元符号,然后您需要小数点后的两个选项数字:
var r = /^$?[0-9]+\.?[0-9]*$/;
console.log(r.test("12.55")); //true
console.log(r.test(".55")); //true
console.log(r.test(".")); //true
console.log(r.test("")); //true
console.log(r.test(".555")); //true
console.log(r.test("$")); //false
Or, to allow arbitrary digits after the decimal point, you could use a Kleene closure instead of two optional digits
或者,要允许小数点后的任意数字,您可以使用 Kleene 闭包而不是两个可选数字
/^$?\d+(,\d{3})*\.?[0-9]?[0-9]?$/
回答by user2449231
For those of us that want to allow for a comma, here is the regex for that:
对于我们这些想要允许逗号的人,这里是正则表达式:
/^ *$?\d+(?:\.\d{2})? *$/g
I advocate allowing it and removing the commas on the backend if you don't actually want them. Anything to make it easier for the user. Jakob Nielson discusses cumbersome forms at point #7.
如果你真的不想要它们,我主张允许它并删除后端的逗号。任何使用户更容易的东西。 Jakob Nielson 在第 7 点讨论了繁琐的表格。
回答by mathematical.coffee
I'd recommend doing the validation not on .keyup
(ie as the user types), but when the user submits the form.
我建议不要在.keyup
(即在用户键入时)进行验证,而是在用户提交表单时进行验证。
Then instead of replacing invalid characters, you can just verify that the dollarAmountOnly
textbox matches the right regex.
然后,您可以验证dollarAmountOnly
文本框是否与正确的正则表达式匹配,而不是替换无效字符。
A regex for valid price is (this allows trailing/leading spaces):
有效价格的正则表达式是(这允许尾随/前导空格):
_this.currencyRegex = /^($|\£|\?|)(?!0\.00)\d{1,3}(,\d{3})*(\.\d\d)?$/;
So when the user presses submit, you see if the regex matches dollarAmountOnly
, and if not then let the user know somehow.
因此,当用户按下提交时,您会看到正则表达式是否匹配dollarAmountOnly
,如果不匹配,则以某种方式让用户知道。
回答by shacharsol
Here is a regex i used to identify various currencies in javascript:
这是我用来在 javascript 中识别各种货币的正则表达式:
##代码##and a good tool for debug:
和一个很好的调试工具: