I want to check to see if a user entered the correct number of decimal points. In the database the amount is DECIMAL(4,2) . How would I enforce this? Acceptable input: 1 1.1 1.11 11.11
One option would be to simply force it:
$value = round($value, 2);
If you want to validate it, one possibility would be:
$value = trim($value); if(preg_match('/^\d+(\.\d{1,2})?$/', $value) { // valid } else { // NOT valid }