Regex验证字母,点划线Laravel


Regex validation letters, dots and dash Laravel

我有这个验证:

'users.first_name' => 'required|min:2|regex:/[A-Za-z. -]/|max:255',

为什么此验证通过此名称:John[][]

有人在评论中也指出了这个问题,即/[A-Za-z. -]/不关心所有字符,但表示如果要验证的字段只有最少的字符,这对我来说就足够了。

要只包含这些字符,您应该使用插入符号^$:指定输入文本的开头和结尾

regex:/^[A-Za-z. -]+$/

使用Alpha破折号验证

'users.first_name' => 'required|min:2|alpha_dash|max:255',