I'd like to validate a numeric field (price) in my form.
I try in this way to validate format like 10.00
and it's ok.
$pattern = '/^\d+(:?[.]\d{2})$/';
if (preg_match($pattern, $_POST['price']) == '0') {
echo "ERROR";
exit;
}
Now I'd like to validate, at the same time, the field format like 10.00
and 10
.
How could I do this?
Your new pattern:
$pattern = '/^\d+(\.\d{2})?$/';
will validate:
10
10.00
If you want to invalidate zero-leading numerics such as 05.00, the following pattern will help:
$pattern = '/^(0|[1-9]\d*)(\.\d{2})?$/';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With