Validate only alphanumeric characters in Laravel

后端 未结 4 573
野的像风
野的像风 2021-02-08 17:26

I have the following code in my Laravel 5 app:

public function store(Request $request){
    $this->validate($request,          


        
4条回答
  •  孤城傲影
    2021-02-08 18:15

    You need to make sure the pattern matches the whole input string. Also, the alphanumeric and an underscore symbols can be matched with \w, so the regex itself can be considerably shortened.

    I suggest:

    'regex:/^[\w-]*$/'
    

    Details:

    • ^ - start of string
    • [\w-]* - zero or more word chars from the [a-zA-Z0-9_] range or -s
    • $ - end of string.

    Why is it better than 'alpha_dash': you can further customize this pattern.

提交回复
热议问题