Validate input is decimal number - input field for currency exchange rate

Viewed 119

I have input field for currency exchange rate.

How do i check, on the server side, that value is not bigger then 2 digits before the decimal point and not more then 2 after?

in sort, the number can be maximum as 99.99

<input type="number" name="cc" value="" step=".01" min="0" max="10">
1 Answers

You can use regular expression approach in this case:

<?php
$rate = $_REQUEST['cc']; //'9.99';
if (preg_match("/^\d{1,2}$|^\d{1,2}\.\d{0,2}$/i", $rate)) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

PHP Sandbox code example

Related