Laravel Validation: No space for username

I have a little problem in laravel validation request. I want to reject the username with a space, like foo bar . I just want to allow foobar without spaces. Right now my rule is required|unique:user_detail,username . Which rule should I use? thanks

+5
source share
3 answers

You can extend the validator using your own rules:

 Validator::extend('without_spaces', function($attr, $value){ return preg_match('/^\S*$/u', $value); }); 

Then just use it like any other rule:

 required|without_spaces|unique:user_detail,username 

Place an order for documents according to user verification rules:

https://laravel.com/docs/5.2/validation#custom-validation-rules

+8
source

Why aren't you using alpha_dash rule ?

 required|alpha_dash|unique:user_detail,username 

From the documentation:

A field under validation can have alpha-numeric characters, as well as dashes and underscores.

And that does not allow spaces.

+12
source

You must use regex with your validation.

PHP:

 required|unique:user_detail,username,'regex:/\s/' 
+1
source

All Articles