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
Asked
Active
Viewed 1,360 times
1
-
You should create your own rule maybe using regex. See [docs for creating a new rule](https://laravel.com/docs/8.x/validation#custom-validation-rules). – Marinario Agalliu Oct 05 '21 at 08:00
-
/[A-Z]{2}[0-9]{4}/m ..Use Laravel validator's regex validation method to validate your value – Vivek Choudhary Oct 05 '21 at 08:11
2 Answers
1
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).

hms5232
- 323
- 1
- 6
0
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

adam
- 347
- 2
- 7