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?
JavaScript
x
public function rules()
{
return [
'name'=>'required',
'password'=>'required|min:6',
'phone'=>'required|digits:9',
];
}
Advertisement
Answer
One possible solution would to use regex.
JavaScript
'phone' => ['required', 'regex:/^((71)|(73)|(77))[0-9]{7}/']
- I assume your phone number has 9 digits so I used {7} // (71|73|77)2 digits + 7 digits = 9 digits
- Notice when you want to use OR, you should use an array instead of separating rules using |