Validate on Laravel 5.4 username only English letters and numbers
'username' => 'required|string|without_spaces|max:255|unique:users|regex:/(^([a-zA-Z]+)(\d+)?$)/u',
You can just add alpha
validation and it will do the trick.
'username' => 'required|alpha|max:255|unique:users',
you have two options:
option A: You should simply put this regex in the validation method like:
'username' => ['required', 'string', 'max:255', 'unique:users',
'regex:/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/'],
option B:
alpha_dash
is a default Laravel validation rule which allows letters, numbers, dashes and underscores and NOT space.
When you implement alpha_dash
rule, the field under validation may have alpha-numeric characters, as well as dashes and underscores:
'username' => ['required', 'string', 'max:255', 'unique:users', 'alpha_dash'],