如何检查输入的值是否为货币 [英] How to check if an entered value is currency

查看:91
本文介绍了如何检查输入的值是否为货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查输入的值是否为货币.优选通过正则表达式或php函数.

How to check if an entered value is currency. Preferably by regular expression or php function.

(诸如1550.501500100.75的值)

推荐答案

我想您真正想要的是一种从有意义的数字告诉任何数字的方法作为货币值.所以1.04e-7可能不应该匹配,1.23412.3也不应该匹配,即使它们都是数字.

I guess what you really want is a way to tell any number from a number that makes sense as a currency value. So 1.04e-7 probably shouldn't match, and neither should 1.234 or 12.3, even though all are of course numeric.

最后,您必须期望像1,234.56这样的数千个分隔符(像小数点一样,在区域设置之间也可能有所不同).因此,假设您只想使用点作为小数点分隔符和逗号作为可选的千位分隔符来检查货币值,请尝试以下操作:

Finally, you'd have to expect thousands separators like 1,234.56 (which, like the decimal point, might also vary between locales). So, assuming that you only ever want to check currency values using the dot as decimal separator and the comma as an optional thousands separator, try this:

/\b\d{1,3}(?:,?\d{3})*(?:\.\d{2})?\b/

说明:

\b      # word boundary assertion
\d{1,3} # 1-3 digits
(?:     # followed by this group...
 ,?     # an optional comma
 \d{3}  # exactly three digits
)*      # ...any number of times
(?:     # followed by this group...
 \.     # a literal dot
 \d{2}  # exactly two digits
)?      # ...zero or one times
\b      # word boundary assertion

这篇关于如何检查输入的值是否为货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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