ITECH ITECH - 5 months ago 8
PHP Question

PHP Regex Not Quite Working

I am using the following regex:

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


I use this regex to check for valid prices -- so it catches/rejects things like
xxx
, or
llddd
or
34.23dsds

and allows things like
100
or
120.00

The problem with it seems to be if it is blank(empty) it passes as valid which it should not -- any ideas how to change this??

Thanks

Answer

One of your problems is that you use the dot in your regex which stands for "any character". If you mean a dot you need to escape it like this \.

Also you should have at least one number in it so exchange the asterisk * by a + for "one or more".

Then you can have .,.,.,.,.,.,- if you do not remove the comma and dot from the first part:

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