开发者

Validate a number with grouped thousands with decimal to the hundreth

Looking for a simple way (Function/RegEx) to validate a number with grouped thousands.

Example Numbers:

.00 - 999.00 should validate
1,000.00 should validate
100,000.00 etc... should validate
100,000,000,000,000.00 should validate

Now I've seen the number_format(), but this formats the number not validates.

I wanted to use a RegEx but lost on how to do so.

preg_match(/^[\d]\开发者_StackOverflow中文版,?\.[\d]{2}$/, $number);

but this doesn't work.

I've also looked at the money_format() but again this is format and not validation.


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


Just off the top of my head:

preg_match('%^[\d,]*\.\d{2}$%', $number);

This will match all of the numbers you mentioned (in fact: every string which starts with a combination of digits and commas and ends on "." and to digits).

Again, this is untested but should work.


If you've got a PHP5.3+ you could try the Intl number formatter:

http://www.php.net/manual/en/numberformatter.parse.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜