phplaravelregex-negation

Laravel string validation; all char except #


I have a Laravel 10 project and I'm trying to validate a string that can include any character (max 11 chars), except the # char

In my controller I tried this code, using regex

$request->validate([
    'code' => 'required|max:11|regex:/[^#]*/',
]);

but it doesn't work; if I wrote for example "CG#777" it accept the string, but I want it to discard.

Anyone can help me?


Solution

  • Try this one

    $request->validate([
        'code' => 'required|string|max:11|regex:/^[^#]+$/'
    ]);