Reputation: 37
I want to check my phone field in my form in laravel 5 with this RegEx validation rule:
(\+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.
Upvotes: 0
Views: 6017
Reputation: 559
You can use Laravel-Phone package for Validation, Formatting and more functionality.
In your case you can specify you country like this :
'phone' => 'required|phone:BG'
BG = Bulgaria
Upvotes: 1
Reputation: 61
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.
Upvotes: 1
Reputation: 13562
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})$/
Upvotes: 0