Is there's a way to prevent negative numbers but accept decimals? I'm using PHP and HTML
<input type="number" min="0" id="Student" name="course[]" value="<?php echo $_POST['course'][3]; ?>"/></p>
Is there's a way to prevent negative numbers but accept decimals? I'm using PHP and HTML
<input type="number" min="0" id="Student" name="course[]" value="<?php echo $_POST['course'][3]; ?>"/></p>
Your code theoretically works. It prevents negative numbers and accepts decimals.
I think what you're really asking is this: Is there a float input type in HTML5?
to which the top answer suggests that you would write:
<input type="number" min="0" step="0.01" name="course[]">
The thing that matters the most though is not client-side validation, but server-side validation. In the PHP code that accepts this input, you have to validate that the input is valid as follows:
if(is_numeric($_POST['course'][3]) && floatval($_POST['course'][3]) >= 0) {
// The rest of your code.
}
To do this for all the courses, you would most likely write:
foreach($_POST['course'] as $k => $v) {
if(!(is_numeric($v) && floatval($v) >= 0)) {
echo "Input needs to be a number.";
break;
}
// Process the course $_POST['course'][$k] here.
}
The main reason you have to check if it's a number in server-side too is that someone with certain knowledge about browsers can just open the browser console and edit the attributes of the input removing the type="number", allowing him to send you virtually any text.
Someone with even more knowledge can send you cURL requests.