How can input validation be improved to only accept numbers from 0 to 99 in a PHP form?
To improve input validation in a PHP form to only accept numbers from 0 to 99, you can use a combination of HTML input type "number" with min and max attributes, along with PHP validation to ensure the input falls within the specified range. This way, the form will only accept numeric values between 0 and 99.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST["number"];
if (is_numeric($number) && $number >= 0 && $number <= 99) {
// Number is valid
echo "Number is valid: " . $number;
} else {
// Number is not within the range
echo "Please enter a number between 0 and 99.";
}
}
?>
<form method="post">
<label for="number">Enter a number between 0 and 99:</label>
<input type="number" id="number" name="number" min="0" max="99" required>
<button type="submit">Submit</button>
</form>