Home > Enterprise >  Laravel form validation set rules like first two character are alphabet and last four character nume
Laravel form validation set rules like first two character are alphabet and last four character nume

Time:10-05

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

CodePudding user response:

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).

CodePudding user response:

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

  • Related