Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel, valdiation alpha_dash prevent dashes

Tags:

php

laravel

I have validation rule:

$rules = ['username' => 'required|string|alpha_dash']

I need prevent dash in validation, allow only underscores, letters and numbers. How I can do it? Now alpha_dash allow dashes..

like image 543
Mafys Grif Avatar asked Dec 22 '22 23:12

Mafys Grif


1 Answers

I would suggests to use regex validation to get more power to customize in future if you wish. SEE https://laravel.com/docs/5.8/validation#rule-regex

'regex:/^[A-Za-z0-9_]+$/'

or more specifically

$rules = ['username' => 'required|string|regex:/^[A-Za-z0-9_]+$/']

Because as per documentation alpha_dash supports-

The field under validation may have alpha-numeric characters, as well as dashes and underscores.

like image 113
Always Sunny Avatar answered Jan 10 '23 00:01

Always Sunny