2016-09-06 56 views
1

我這樣從創建:White Spaces頻段驗證5.2

{!! Form::text('name', null, [ 
       'class' => 'form-control', 
       'placeholder'=>'Name', 
       "required" => "required|regex:/(^[A-Za-z0-9]+$)+/", 
       'maxlength' => 55, 
       'minlength' => 5     
       ]) 
!!} 

我想,以確保用戶能超過五次輸入不只是空白空間更多。但是這個正則表達式:/(^ [A-Za-z0-9] + $)+ /不起作用。每次我輸入空間超過5次時,它總是有效的。那麼如何防止這件事...?

我試過'field'=> 'regex:/(^[A-Za-z0-9 ]+$)+/'從此鏈接:Laravel - Validate only letters, numbers and spaces using regex。它沒有工作對我來說

回答

2

我終於找到了答案:

{!! Form::text('name', null, [ 
     'class' => 'form-control', 
     'placeholder'=>'Name', 
     "required" => 'required', 
     'maxlength' => 55, 
     'minlength' => 5, 
     'pattern' => ".*\S+.*" 
     ]) 
    !!} 

所以我只需要添加'pattern' => ".*\S+.*"然後空格/空白將被視爲無效的輸入。