waqar
waqar

Reputation: 33

Laravel form validation set rules like first two character are alphabet and last four character numeric like AB1234

Please help me I am new in laravel. I set form validation like first two characters are alphabet and last four character numeric. Example AB1234

Upvotes: 1

Views: 1951

Answers (2)

adam
adam

Reputation: 367

Use Regular expression in the Validator,

return Validator::make($data, [
    'your_input' => [
        'required',
        'regex:/^[a-zA-Z]{2}[0-9]{4}+$/',
    ]
]);

you need to add ^ and $ on regex to make sure it match First 2 and 4 End

Upvotes: 1

hms5232
hms5232

Reputation: 333

As comments say, you can do this by regex:

$validated = $request->validate([
    'some_col' => ['string', 'regex:/^[A-Z]{2}[0-9]{4}$/'],
    // other rule
]);

According to docs, this rule use preg_match so you should follow the same formatting required by preg_match. Don't forget to use array to specify rules (just like example code).

Upvotes: 1

Related Questions