0

I'm trying to check if a sentence contains the special character | or ] using Laravel/Lumen validation in the controller like below:

'to_address' => 'required|max:200|regex:/^[^(|]~`!%^&*=};:?><’)]*$/',

If I user this validation on my controller, I'm getting the following error

{
    "error": "preg_match(): No ending delimiter '/' found"
}

Without | and ] the validation is working correctly.

Worthwelle
  • 1,244
  • 1
  • 16
  • 19
nifCody
  • 2,394
  • 3
  • 34
  • 54

2 Answers2

2

Usually array of rules fixes this problem.

'to_address' => ['required', 'max:200', 'regex:/^[^(|]~`!%^&*=};:?><’)]*$/'],
IndianCoding
  • 2,602
  • 1
  • 6
  • 12
0

Both | and ] are special characters in regular expressions, so you need to escape them with the \ character:

'to_address' => 'required|max:200|regex:/^[^(\|\]~`!%^&*=};:?><’)]*$/',

If the pipe is supposed to be an OR, then you will need to use an array, as mentioned in Laravel preg_match(): No ending delimiter '/' found

Further reading:

Worthwelle
  • 1,244
  • 1
  • 16
  • 19