[英]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 .我想验证电话号码字段并只允许以以下数字 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',
];
}
You should just a regex solution here, eg你应该只是一个正则表达式解决方案,例如
var numbers = [771234567, 128675309]; console.log(/^(?:71|73|77)\d{7}$/.test(numbers[0])); console.log(/^(?:71|73|77)\d{7}$/.test(numbers[1]));
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.这将检查输入是否以 01 开头,后跟 9 个数字。 By using regex you don't need the numeric or size validation rules.
通过使用正则表达式,您不需要数字或大小验证规则。
One possible solution would to use regex.一种可能的解决方案是使用正则表达式。
'phone' => ['required', 'regex:/^((71)|(73)|(77))[0-9]{7}/']
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}$/',
];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.