Search code examples
laravellaravel-5laravel-validation

Laravel 5 Validation accept comma separated string with max 4 numbers


Laravel 5 Validation accept comma separated string with max 4 numbers

Example-

1.  1,2,3,4              ---  Accepted

2.  1,2                  ---  Accepted

3.  1,2,3,4,5            ---  Rejected

Note: I can Achieve this task by first convert the string to array and then validate the request, But i am looking for a Best approach to solve the same.


Solution

  • Validate it with this in your controller:

    $this->validate(Request::instance(), ['field_name'=>['required','regex:/^\d+(((,\d+)?,\d+)?,\d+)?$/']]);