douyan4243 2014-07-31 11:20
浏览 63

php条件表单按日期验证

I have the function below that takes an entered date and serves different validation messages base don the dates value .The thing is is not always working as it's supposed. I'm new at PHP and maybe i'm doing something wrong. Anyone has an ideea how to correct it?

function validate_date($result, $value, $form, $field){

$date1=date("Y", strtotime($value));
$date2=date("Y");
$diff= $date2-$date1;
$diff2=18-$diff;


if ($value ==0) { 
    $result["is_valid"] = false;
    $result["message"] = "Compeltati data!";
}
else {
    if ($diff < 0) { 
        $result["is_valid"] = false;
        $result["message"] = "Data invalida!";
    }
    elseif ($diff == 0) {
        $result["is_valid"] = false;
        $result["message"] ="Data invalida!";
    }
    elseif ($diff > 0){
        if ($diff2 > 0 and $diff2 < 5 ){
            $result["is_valid"] = true;
            echo '<script type="text/javascript">alert("Va trebuie confirmarea parintilor. Contactati-ne pe email!");</script>';
        }
        elseif ($diff2 > 5) {
            $result["is_valid"] = false;
            $result["message"] = "Nu aveti varsta minima pentru participare!";
        }
    }
}

return $result;
}
  • 写回答

1条回答 默认 最新

  • dongyan1993 2014-07-31 11:42
    关注

    As I understand $diff is AGE and $diff2 is how much he/she is younger then 18year age. In that case if age of person is more then 18 then $diff2 will be negative and you have'nt added any clause for $diff2<0. so add it like:

    if ($diff2 > 0 and $diff2 < 5 ){ $result["is_valid"] = true; echo '<script type="text/javascript">alert("Va trebuie confirmarea parintilor. Contactati-ne pe email!");</script>'; } elseif ($diff2 > 5) { $result["is_valid"] = false; $result["message"] = "Nu aveti varsta minima pentru participare!"; } else { //for $diff<=0 $result["is_valid"] = false; //as you need change it on age >=18 $result["message"] = "as you need!"; }

    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致