laravel 5 使用 RegEx 验证保加利亚电话号码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31987363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
5 Validation with RegEx for Bulgarian phone numbers
提问by deshi
I want to check my phone field in my form in laravel 5 with this RegEx validation rule:
我想使用以下 RegEx 验证规则检查 laravel 5 表单中的电话字段:
(\+359|0)\s?8(\d{2}\s\d{3}\d{3}|[789]\d{7})
I tried this:
我试过这个:
'phone' => 'required|regex:(\+359|0)\s?8(\d{2}\s\d{3}\d{3}|[789]\d{7})'
But my form doesn't catch this validation. I think that something is missing in my RegEx.
但是我的表单没有捕捉到这个验证。我认为我的 RegEx 中缺少某些内容。
采纳答案by cre8
You need delimiters (in my example, /
) around your regex:
您需要在/
正则表达式周围使用分隔符(在我的示例中为):
'phone' => 'required|regex:/^(\+359|0)\s?8(\d{2}\s\d{3}\d{3}|[789]\d{7})$/
回答by pan85
Just add
只需添加
<?php
return ['phone' => 'required|regex:/^\+?[^a-zA-Z]{5,}$/'];
?>
I think this is fairly enough. If there is no example what is the format, use can add phone number like:
我认为这已经足够了。如果没有例子是什么格式,使用可以添加电话号码,如:
+359878XXX, +359 878 XXX, +359 87 8X XX, +(359) 878 XXX, 0878-XX-XX-XX.
+359878XXX、+359 878 XXX、+359 87 8X XX、+(359) 878 XXX、0878-XX-XX-XX。
回答by Nadjib KHELIFATI
You can use Laravel-Phonepackage for Validation, Formatting and more functionality.
您可以使用Laravel-Phone包进行验证、格式化和更多功能。
In your case you can specify you country like this :
在您的情况下,您可以像这样指定您的国家/地区:
'phone' => 'required|phone:BG'
BG = Bulgaria
BG = 保加利亚