Reputation: 730
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',
];
}
Upvotes: 3
Views: 18138
Reputation: 669
The other answers given are excellent but it depends on the country used as phones varies from country to country for example in Nigeria we validate phone number like
public function rules()
{
return [
'name'=>'required',
'password'=>'required|min:6',
'phone'=>'required|regex:/^(080|091|090|070|081)+[0-9]{8}$/',
];
}
Upvotes: 0
Reputation: 4388
One possible solution would to use regex.
'phone' => ['required', 'regex:/^((71)|(73)|(77))[0-9]{7}/']
Upvotes: 3
Reputation: 34668
You can use regex to validate a phone number like :
'phone' => 'required|regex:/(01)[0-9]{9}/'
This will check the input starts with 01 and is followed by 9 numbers. By using regex you don't need the numeric or size validation rules.
Upvotes: 2
Reputation: 520938
You should just a regex solution here, e.g.
var numbers = [771234567, 128675309];
console.log(/^(?:71|73|77)\d{7}$/.test(numbers[0]));
console.log(/^(?:71|73|77)\d{7}$/.test(numbers[1]));
Upvotes: 2