PHP regular expression to match price or amount

I need a regex that will match on a price or amount.

It should match on these

  • 100
  • 410.00
  • 0.12

but not these

  • 100.000
  • -600.00
  • .12
  • .1234

This works for all of the above cases except for single values like 1

/^[0-9]*\.?[0-9]{2}+$/

How can I adjust it so single integers will match? And can anyone explain why the current one is wrong?

-------------Problems Reply------------

I suggest a small correction to Tom Revell's regular expression since you could still input several 0's as first digits. ie 00002.23 which is most likely incorrect.

/^([1-9][0-9]*|0)(\.[0-9]{2})?$/

Make the fractional part optional:

/^[0-9]+(\.[0-9]{2})?$/

The regular expression you've entered will match the following:

Any digit, 0 or more times, followed by, a dot, 0 or 1 time, followed by exactly 2 digits

So the regular expression will certainly always require at least 2 digits due to the end bit.

I'd suggest the following:

/^[0-9]+(\.[0-9]{2})?$/

Which will match 1 or more digits, optionally followed by a dot and exactly 2 digits.

I think you'd be better off leaving regular expressions out of the mix.

What I would do is convert the value to an int and round off the decimal, then check if it's negative. I would also check whether the first character is a dollar sign and, if so, just remove it. The code would be simpler and more readable that way IMO, and if someone did enter 12.34512 or .1, the system would still work.

//untested

$amount = "$0.12"
if( $amount && $amount[0] == '$' ) {
ltrim($amount, '$');
}
$int_amount = (int) $amount;
$int_amount = round($int_amount, 2);
if( $amount <= 0 ) {
//error
}

That being said, I don't know whether you have to use regexp for whatever reason (like if you have a validation function that takes a value and regexp as arguments), but in a case like this I think being liberal in the price format would be better so as not to tell users that they entered an "invalid amount" when they input 12 cents without a leading zero.

Best way to work out a regular expression is to start out by getting the idea down on paper. Try drawing your regex or writing it in some kind of pseudo code, test it in your head and make sure it's right before you get bogged down in your language's regex syntax.

Category:php Views:0 Time:2010-08-18
Tags: php regex

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.160 (s). 11 q(s)