Fix Laravel preg_match(): No ending delimiter ‘/’ found

Robioki Denis
Sep 15, 2021
Photo by AltumCode on Unsplash

Hello Friend When we are creating some validation, sometimes we need to using regex to filter and validation special char, some case i created validation for indonesian phone numberic format using this regex

^(^\+62|62|^08)(\d{3,4}-?){2}\d{3,4}'phone' => 'required|regex:/^(^\+62|62|^08)(\d{3,4}-?){2}\d{3,4}/|min:10|unique:users,phone,'.$this->user()->id,

and i got error

preg_match(): No ending delimiter '/' found

after read this from official documentation i got solution to fix this error

nb : Since regex has a pipe in it, you have to use an array;

'phone' => [
'required',
'regex:/^(^\+62|62|^08)(\d{3,4}-?){2}\d{3,4}$/',
'min:10',
'unique:users,phone,' . $this->user()->id
],

regex:pattern

The field under validation must match the given regular expression.

Note: 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.

That’s it….

--

--

Robioki Denis

DevOps Engineer & Fullstack Developer, someone who has a high curiosity 😁