dragon0118 2017-10-21 08:07
浏览 71
已采纳

在php中验证年龄

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>";
}
  • 写回答

4条回答 默认 最新

  • douwen5741 2017-10-21 08:41
    关注

    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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?