I have a little problem in laravel validation request. I want to reject username with space like foo bar
. I just want to allow foobar
without space. Right now my rule is required|unique:user_detail,username
. What rule should i use? thanks
Why don't you use alpha_dash
rule?
required|alpha_dash|unique:user_detail,username
From the documentation:
The field under validation may have alpha-numeric characters, as well as dashes and underscores.
And it doesn't allow spaces.
You can extend the validator with your own custom rules:
Validator::extend('without_spaces', function($attr, $value){
return preg_match('/^\S*$/u', $value);
});
Then just use as any other rule:
required|without_spaces|unique:user_detail,username
Checkout the docs on custom validation rules:
https://laravel.com/docs/5.2/validation#custom-validation-rules
You should use regular expression with your validation.
PHP :
required|unique:user_detail,username,'regex:/\s/'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With