Search code examples
laravelvalidation

What is the number validation if I want the user input should be either 9 digit or 12 digit or 15 digit in laravel?


In Laravel form input validation, how can I check user to force input number of either 9 digit or 12 digit or 15 digit?

following this suggession I tried required|regex:/^(\d{9}|\d{12}|\d{15})$|unique:users,id_no. it shows error.


Solution

  • When validating using regex it is recommended to use an array to separate validation rules e.g.:

    $rules = [
       'myfield' => [ 'required', 'regex:/^(\d{9}|\d{12}|\d{15})$/', 'unique:users,id_no' ]
    ]
    

    This is because there is a conflict between the | used to separate validation rules and the one used as a regex "OR"