I have tested and implemented regex in laravel but it show me the error. Please look into this
EmployerSignupRequest.php
'ephone' => 'required|regex:/^[((\+)|0)[.\- ]?[0-9][.\- ]?[0-9][.\- ]?[0-9]+$/',
and i am getting this error.
preg_match(): No ending delimiter '/' found' in /var/www/html/jobma/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:1370
when i run this in php, This is working fine
<?php
$subject = "+91-9041742722";
$pattern = '/^[((\+)|0)[.\- ]?[0-9][.\- ]?[0-9][.\- ]?[0-9]+$/';
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
I think you are missing syntax here.You can use below pattern for your regular expression.
array('phone' => 'required', 'required|regex:/^\+?[^a-zA-Z]{5,}$/');
First of all, you need to use an array because your pattern contains a pipe. See the docs:
When using the
regex
pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
Also, your pattern starts with [((\+)|0)[.\- ]?
optional character class that matches either (
or +
, )
, |
, 0
, [
, -
, or space. I think you want to match either a 0
or a +
optionally followed by a delimiter, so replace it with [0+][.\- ]
.
Use
'ephone' => ['required', 'regex:/^[0+][.\- ]?[0-9][.\- ]?[0-9][.\- ]?[0-9]+$/'],
If the +
or 0
are to be optional, add ?
after [0+]
(this matches either a 0
or a literal +
, and if you add a ?
after it, this will be optional, matching 1 or 0 occurrences of the pattern).
You can further test the regex at the regex101.com online regex tester.