Just wondering if I am allowed to validate age this way. So the user must enter an age, there age must be a number and they must be between 15 to 80
if ($age=="") {
$errMsg .= "<p>You must enter your age.</p>";
} else if (!is_numeric($var),$age)) {
$errMsg .= "<p>Your age must be a number.</p>";
} else if ($age > 14) {
$errMsg .= "<p>You must be 15 or older to apply for this job.</p>";
} else if ($age < 81) {
$errMsg .= "<p>You must be 80 or younger to apply for this job.</p>";
}
I don't think you'd need else if statements here.
An if else check for not empty $age and is_numeric $age doesn't really make sense (they are essentially 2 seperate checks, rather than one conditional or the other).
You can do the 4 seperate checks on $age like below:
// Check if $age is not empty
if (empty($age)) {
$errMsg .= "<p>You must enter your age.</p>";
}
// Check if age is not empty and also not numeric
if (!empty($age) && !is_numeric($age)) {
$errMsg .= "<p>Your age must be a number.</p>";
}
// Check if age is not empty and is under 14
if (!empty($age) && $age < 14) {
$errMsg .= "<p>You must be 15 or older to apply for this job.</p>";
}
// Check if age is not empty and is over 80
if (!empty($age) && $age > 81) {
$errMsg .= "<p>You must be 80 or younger to apply for this job.</p>";
}
Your syntax is incorrect
if ($age=="") {
$errMsg .= "<p>You must enter your age.</p>";
} else if (!is_numeric($age)) {
$errMsg .= "<p>Your age must be a number.</p>";
} else if ($age < 14) {
$errMsg .= "<p>You must be 15 or older to apply for this job.</p>";
} else if ($age > 81) {
$errMsg .= "<p>You must be 80 or younger to apply for this job.</p>";
}
I think:
if ($age=="") {
$errMsg .= "<p>You must enter your age.</p>";
} else if (!is_numeric($age)) {
$errMsg .= "<p>Your age must be a number.</p>";
} else if ($age < 14) {
$errMsg .= "<p>You must be 15 or older to apply for this job.</p>";
} else if ($age > 81) {
$errMsg .= "<p>You must be 80 or younger to apply for this job.</p>";
}
Just convert the input to int before checking, like this:
$age = (int)$age;
if ($age < 14 || $age > 80)
$errMsg .= "<p>You must be between 15 and 80 years old to apply for this job</p>";