PHP表单验证最小字符需要避免空白?

时间:2016-07-22 05:24:51

标签: javascript php validation

在我们的人力资源系统中,我放了一个验证用户必须要记下最少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();
}

2 个答案:

答案 0 :(得分:6)

尝试修剪字符串,如W3 Schools js trim example

我没有权利发表评论,这就是我发布这个答案的原因......

希望这可以帮助你..

再次由@Julie Pelletier评论它实际上并没有完全解决这个问题..! :d

谢谢..!

答案 1 :(得分:2)

  

使用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();
 }