Page 1 of 1

Using ereg to validate a price - EREG CHALLENGE

Posted: Tue Jul 19, 2005 9:21 am
by johnali3n
Hey all,

I am trying to use ereg to validate a price. I am asking users to enter a price for a product in a text box - I do not want to allow anything but 0-9 and a period/full stop.

Can anyone help me with the ereg syntax? I would of thought it would be something like:-

(!ereg ("^[0-9].[0-9]$"), $price))

But this wont work, because there can be more than one digit before and/or after the period/full-stop.

Please help, thanks :)

Posted: Tue Jul 19, 2005 9:25 am
by Chris Corbyn
I tend to go with preg_ over ereg_... it's much much better if you get used to it ;)

Code: Select all

//This requires the period and two more digits!
preg_match("/^[0-9]+.[0-9]{2}$/", $price);

//This one will accept the decimal part but it's optional
preg_match("/^[0-9]+(.[0-9]{2})?$/", $price);
ereg way you were almost there ;)

Code: Select all

ereg("^[0-9]+.[0-9]{2}$", $price);

Posted: Tue Jul 19, 2005 9:27 am
by johnali3n
Excellent, thanks very much!!!!!!!!! :lol: :lol: 8O 8O