使用JavaScript中的正则表达式验证货币金额 [英] Validate currency amount using regular expressions in JavaScript

查看:127
本文介绍了使用JavaScript中的正则表达式验证货币金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

什么是C#正则表达式,可以验证货币,浮点或整数?


如何使用JavaScript中的正则表达式验证货币金额?



小数点分隔符:



数十,数百等分隔符:



模式: ###。###。## #,##



有效金额示例:

  1 
1234
123456

1.234
123.456
1.234.567

1,23
12345,67
1234567,89

1.234,56
123.456,78
1.234.567,89

编辑



我忘了以下模式是同样有效: ###,###,###。##

解决方案

仅基于您给出的标准,这就是我想出的。 p>

/(?:^ \d {1,3}(?: \。?\d {3})*(?: ,\d {2})?$)|(?:^ \d {1,3}(?:,?\d {3})*(?: \.\d {2}) ?$)/



http:/ /refiddle.com/18u



这很丑陋,而且只会随着您发现更多需要匹配的情况而变得更糟。



更新为反映出增加的要求。






有关以下评论,再次进行了更新。



它将匹配 123.123,123 (三个尾随数字,而不是两个),因为它将接受逗号或句点作为千位和十进制分隔符。为了解决这个问题,我现在基本上将表达式加倍了;或者将整个事物与逗号分隔符和一个句点作为基数相匹配,或者将整个事物与逗号分隔符和一个逗号作为基数点。



明白我变得越来越混乱的意思吗? (^ _ ^)






以下是详细说明:

 (?:^#字符串的开头
\d {1,3}#一,两位或三位数
(?:
\ ?。?#可选的分隔时间
\d {3}#后跟正好三位数
)*#重复此子模式(。###)任意次(包括完全不重复)
(?:,\d {2})?#(可选)后跟一个十进制逗号和正好两位数字
$)#字符串的结尾。
| #...或...
(?:^#字符串
\d {1,3}的开头#一,两位或三位数
(?:
,?#可选的逗号分隔
\d {3}#后跟精确的三位数
)*#重复此子模式(,###)任意次(包括完全不重复)
(?:\.\d {2})?#(可选)后跟一个十进制句点a和正好两位数字
$)#字符串结尾。

让它看起来更加复杂的一件事就是所有?:在那里。通常,正则表达式也捕获(返回匹配项)所有子模式。据说所有?:都不会费心去捕获子模式。因此,从技术上讲,如果您将所有?:取出来,则完整的字符串仍将与您的整个字符串匹配,这看起来会更清楚:



/(^^ d {1,3}(\。?dd {3})*(,\d {2})?$)|( ^ \d {1,3}(,? \d {3})*(\.\d {2})?$)/



此外, regular-expressions.info 是一个很好的资源。


Possible Duplicate:
What's a C# regular expression that'll validate currency, float or integer?

How can I validate currency amount using regular expressions in JavaScript?

Decimals separator: ,

Tens, hundreds, etc. separator: .

Pattern: ###.###.###,##

Examples of valid amounts:

1
1234
123456

1.234
123.456
1.234.567

1,23
12345,67
1234567,89

1.234,56
123.456,78
1.234.567,89

EDIT

I forgot to mention that the following pattern is also valid: ###,###,###.##

解决方案

Based solely on the criteria you gave, this is what I came up with.

/(?:^\d{1,3}(?:\.?\d{3})*(?:,\d{2})?$)|(?:^\d{1,3}(?:,?\d{3})*(?:\.\d{2})?$)/

http://refiddle.com/18u

It is ugly, and it will only get worse as you find more cases that need to be matched. You'd be well served to find and use some validation library rather than try to do this all yourself, especially not in a single regular expression.

Updated to reflect added requirements.


Updated again in regard to comment below.

It would match 123.123,123 (three trailing digits instead of two) because it would accept either comma or period as both the thousands and decimal separators. To fix that, I've now essentially doubled up the expression; either it matches the whole thing with commas for separators and a period as the radix point, or it matches the whole thing with periods for separators and a comma as the radix point.

See what I mean about it getting messier? (^_^)


Here's the verbose explanation:

(?:^           # beginning of string
  \d{1,3}      # one, two, or three digits
  (?:
    \.?        # optional separating period
    \d{3}      # followed by exactly three digits
  )*           # repeat this subpattern (.###) any number of times (including none at all)
  (?:,\d{2})?  # optionally followed by a decimal comma and exactly two digits
$)             # End of string.
|              # ...or...
(?:^           # beginning of string
  \d{1,3}      # one, two, or three digits
  (?:
    ,?         # optional separating comma
    \d{3}      # followed by exactly three digits
  )*           # repeat this subpattern (,###) any number of times (including none at all)
  (?:\.\d{2})? # optionally followed by a decimal perioda and exactly two digits
$)             # End of string.

One thing that makes it look more complicated is all the ?: in there. Normally a regular expression captures (returns matches for) all of the subpatterns too. All ?: does is say to not bother to capture the subpattern. So technically, the full thing would still match your entire string if you took all of the ?: out, which looks a bit clearer:

/(^\d{1,3}(\.?\d{3})*(,\d{2})?$)|(^\d{1,3}(,?\d{3})*(\.\d{2})?$)/

Also, regular-expressions.info is a great resource.

这篇关于使用JavaScript中的正则表达式验证货币金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆