Using ereg to validate a price - EREG CHALLENGE

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
johnali3n
Forum Newbie
Posts: 7
Joined: Tue Jul 19, 2005 9:16 am

Using ereg to validate a price - EREG CHALLENGE

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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);
johnali3n
Forum Newbie
Posts: 7
Joined: Tue Jul 19, 2005 9:16 am

Post by johnali3n »

Excellent, thanks very much!!!!!!!!! :lol: :lol: 8O 8O
Post Reply