Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex validation letters, dots and dash Laravel

Tags:

regex

php

laravel

I have this validation:

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

Why this validation pass this name: John[][]

like image 821
Sebastian Corneliu Vîrlan Avatar asked Dec 15 '22 06:12

Sebastian Corneliu Vîrlan


1 Answers

Someone in comments also pointed to this issue that by /[A-Za-z. -]/ you don't care about all characters but saying that's enough for me if field under validation has only the least of that characters.

To have only those characters you should specify beginning and ending of the input text by using a caret ^ and $:

regex:/^[A-Za-z. -]+$/
like image 162
revo Avatar answered Dec 24 '22 10:12

revo