Skip to content
Advertisement

How to validate phone number in laravel?

I want to validate the field of phone number and allow only numbers that start with the following digits 77, 71 , 73 .

How can I implement that in the request that I created?

public function rules()
    {
        return [
            'name'=>'required',
            'password'=>'required|min:6',
            'phone'=>'required|digits:9',
        ];
    }

Advertisement

Answer

One possible solution would to use regex.

 'phone' => ['required', 'regex:/^((71)|(73)|(77))[0-9]{7}/']
  1. I assume your phone number has 9 digits so I used {7} // (71|73|77)2 digits + 7 digits = 9 digits
  2. Notice when you want to use OR, you should use an array instead of separating rules using |
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement