Php表单验证的最小字符需要避免空白

Php form validation minimum character need avoid white space?

本文关键字:空白 字符 表单 验证 Php      更新时间:2023-09-26

在我们的人力资源系统中,我放了一个验证用户必须至少放8个字母的注释。但如果他们不放,他们就不能打卡。但他们正在做的是放8个空白,他们可以打卡:(所以我想限制他们放钞票,避免空白

if(empty($openPunch)){
            $currentTime = $req->time;
            if (strtotime($currentTime) >= strtotime('09:30')){
                if(strlen($req->note) < 8){
                $time = (strtotime($currentTime) - strtotime('09:30'));
                $minutes = floor($time / 60);
                $minute = $minutes%60;
                $hours = floor($time / 3600);
                return new IceResponse(IceResponse::ERROR,"Today you are late ".$hours.":".$minute." Hours    You Can't Punch in without Fill the Correct Reason");
    }
    }
    $openPunch = new Attendance();
}

尝试修剪字符串,如W3 Schools js修剪示例

我没有发表评论的特权,这就是为什么我发布这个作为一个答案。。。

希望这对你有帮助。。

正如@Julie Pelletier评论的那样,它实际上并没有完全解决问题…!:D

谢谢。。!

使用trim()函数

trim()函数从字符串两侧删除空白和其他预定义字符。


http://www.w3schools.com/php/func_string_trim.asp

      if(empty($openPunch)){
        $currentTime = $req->time;
        if (strtotime($currentTime) >= strtotime('09:30')){
            if(strlen(trim($req->note)) < 8){
            $time = (strtotime($currentTime) - strtotime('09:30'));
            $minutes = floor($time / 60);
            $minute = $minutes%60;
            $hours = floor($time / 3600);
            return new IceResponse(IceResponse::ERROR,"Today you are late ".$hours.":".$minute." Hours    You Can't Punch in without Fill the Correct Reason");
     }
    }
     $openPunch = new Attendance();
 }