Page 1 of 1

regex problem

Posted: Fri Dec 15, 2006 2:51 am
by sh33p1985
ok, i confess, regex is my achillies heel! ive been getting better at it but ive become a bit unstuck in my aim to validate certain fields in some form data.

basically i was trying to verify a price field, only allowing the format:

optional £
one or more digits
"."
2 digits

e.g. £1.00, 1.00 ok
1.0 not ok

i got as far as verifying the x.xx part of the string but im not entirely happy with the code as 1.11111 matches because 1.11 appears in the string as a substirng.

also, another simliar field i have needs to accept numeric values in this form:

25 or 25.0 or 25.00

any help would be much appreciated.

thanks

Posted: Fri Dec 15, 2006 10:54 am
by Kieran Huggins

Code: Select all

$pattern = "/^(£)?(\d*)(\.(\d\d))?$/";
will accept: £25, £25.00, 25, 25.00

Code: Select all

$pattern = "/^(\d*)(\.(\d){1,2})?$/";
will accept: 25, 25.0, 25.00

Cheers,
Kieran