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 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启