remove space from username in laravel rule
:
'username' => ['required', 'string', 'max:255', 'unique:users', 'regex:/^\S*$/u'],
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'],
Pay attention that the usual style of validation in Laravel 5.7, 5.8 is a little different with other older versions
Laravel 5.7,5.8:
return Validator::make($data, [ 'A' => ['required', 'string'], 'B' => ['required', 'string', 'max:255'], ]);
Older versions:
return Validator::make($data, [ 'A' => 'required|string', 'B' => 'required|string|max:255', ]);
If you use alpah_dash and enter some value with some spaces at the end or beginning, it trims and passes the validation and works perfectly.
Comments
Post a Comment