AngularAngularAngular
AngularAngularAngular

Reputation: 3769

Laravel Rule that allows only alpha numeric with - (hiphen)

Here is my Rule for Taxi Code

'TaxiCode' =>  array('required'),

It should accept Alpha Numeric with Hiphen so i added

'TaxiCode' =>  array('required', 'regex:/^-/'),

When i add the alpha_num

'TaxiCode' =>  array('required', 'regex:/^-/', 'alpha_num'),

It shows the number is invalid

The input i given is

BMW - 1902

Upvotes: 3

Views: 5990

Answers (2)

lukasgeiter
lukasgeiter

Reputation: 152860

Your regex matches only one hyphen. You can't combine alpha_num with regex like that.

Simply use a this regex and get rid of alpha_num:

'TaxiCode' =>  array('required', 'regex:/^[a-zA-Z0-9\s-]+$/'),

(By the way, I'd keep the required because then you still get a nice error when nothing is filled in...)

Upvotes: 4

Mohebifar
Mohebifar

Reputation: 3411

You can do it with just regex:

'TaxiCode' => 'regex:/^[A-Za-z0-9\-\s]+$/'

Upvotes: 1

Related Questions