ereg again (hate these things)

Busy's picture

He has: 6,151 posts

Joined: May 2001

This time is money values

What I'm trying to do is determine if the value is infact a dollar value (numbers).
Main problem I have is with thousands of dollars, as the value can be inputted several ways like so:
12,000.00
12,000
12000
12000.00
which are all the same thing.

I've tried the following two styles, but both are proving false
if(!ereg("^([0-9]+|[0-9]{1,3}(\.[0-9]{2})*)(,[0-9]{0,2})?$",$amount))
{ ...

if(!ereg("^[1-9]{1}[0-9]*[.]{1}[0-9]{2}$",$amount))
{ ...

with such a wide range of value, it has to go from 1.00 to 10000000.00 (ten million) with or without the cents value, as the cents value itself can be from nothing to .00 to .99

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

http://www.php.net/ereg has the following, which is what you have, right?

Quote:
Money
eregi('^[1-9]{1}[0-9]*[.]{1}[0-9]{2}$',$price)

The only difference is the single quotes v. double quotes.

Are you stripping, trimming, et cetera as well?

Busy's picture

He has: 6,151 posts

Joined: May 2001

Everything that comes from a form gets trimmed, stripped and slashes added first

I got the second ereg example from (php.net), it works on 12000.00 value but not 12000

**long pause**

By crikey I think I've got it,
if(!ereg("^[0-9]+|[0-9]{1,3}(\.[0-9]{0,2})?$",$amount)) seems to work

It's a bit hard to test properly as I have so many restrictions on everything else, but that with is_numeric() seems to catch everything, wooohooooo

Thanks

Problem with the code above is it will allow values like 1abcdef2, is_numeric() seems to allow multi commas but only one full stop which is great as it allows millions and cents.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Most banks here reject everything that isn't in the right format. You're being very generous to your users!

Busy's picture

He has: 6,151 posts

Joined: May 2001

If I was a bank I'd be very fussy lol

I have a function to force it to a double (include cents value if none) before being inserted into database etc but for this part I don't really need it to validate it.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.