Hi,
I have a simple form with two fields: Number of Hours Worked, and Hourly Wage. The form does what I want it to do, but I am struggling with validating user input. I can validate if the fields are empty, and return error message if that is the case, but I also want to be able to ensure they enter numbers only and return a different error message if they enter any alpha characters.
I tried using is_numeric() to accomplish this but I think I failed somewhere in the nesting of my if...else code.
I have attached the working .php that only validates for input, please help with adding validation for numbers only!
Thanks!
<!DOCTYPE html><html><head><title>Paycheck Calculation</title></head><body><?php $hours = validateInput((int) $_POST['Hours'], "Number of Hours Worked"); $wage = validateInput((int) $_POST['Wage'], "Hourly Wage"); $overtime = max($hours - 40, 0);$pay += $overtime * $wage * 1.5;$pay += ($hours - $overtime) * $wage;if($hours > 40) { //Calculate time and a half echo "<b>This is your gross salary PLUS time and a half: </b>" . $pay; } else if($hours > 0 && $hours < 40) { //Calculate straight hourly echo "<b>This is your gross salary: </b>" . $pay; } if($errorCount > 0) echo "<br />n <b>Please use the "Back" button to re-enter the data.</b><br />n";function displayRequired($fieldName) { echo "The field "<i>$fieldName</i>" is required.<br />n";}function validateInput($data, $fieldName) { global $errorCount; if(empty($data)) { displayRequired($fieldName); ++$errorCount; $retval = ""; } else { $retval = trim($data); $retval = stripslashes($retval); } return($retval);}$errorCount = 0;?> </body></html>