Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Can I make Laravel string length validation count new line on Windows Server as a single character?

Hi,

I have a string length validation for a textarea in the frontend, but it mistmatches the Laravel string validation:

"textarea" => ['required', 'string', 'max:50'],

Because the Windows Server sees a new line as 2 characters: \r\n, and the frontend (with JS) sees it as a single character \n.

Is there a way to make them match?

Thanks

0 likes
6 replies
Tray2's avatar

The best would be to switch to a Linux host, and run apache or nginx, but I think you mentioned it wasn't an option.

2 likes
Ligonsker's avatar

@Tray2 Yep it's a problem right now :/

I made a workaround - instead of using Laravel's validation, I am using vanilla PHP - I am replacing every occurence of \r\n with \n, then I check the string length:

if (mb_strlen(str_replace("\r\n", "\n", $request->textarea)) > 50) {
    return response()->json("Must be less than 50 characters", 400);
}
Tray2's avatar

@Ligonsker You really need to talk to the infra guys, this will give you more problems than it's worth. If they want Laravel, they should host it on Linux. There is a reason that most of the web runs on *nix based servers.

2 likes
martinbean's avatar

I made a workaround - instead of using Laravel's validation, I am using vanilla PHP

@Ligonsker You could instead use middleware that replaces CRLF sequences with just a LF character in the request, before it reaches validation in your form request/controller:

class NormalizeLineEndings
{
    public function handle(Request $request, Closure $next, string ...$fields)
    {
        foreach ($fields as $field) {
            if ($request->filled($field)) {
                // Replace CRLF with LF
                $value = str_replace("\r\n", "\n", $request->input($field));

                // Replace lone CR with LF
                $value = str_replace("\r", "\n", $request->input($field));

                $request->merge([
                    $field => $value,
                ]);
            }
        }

        return $next($request);
    }
}

You can now validate your fields knowing they only include single LF characters.

2 likes
Ligonsker's avatar

@Tray2 I know, I did speak a few times but things move slowly there (a big organization that has been using other stacks and wasn't prepared for that, hopefully that will change)

Ligonsker's avatar

@martinbean That's pretty awesome, tomorrow I will give it a try and let you know how it worked.

Please or to participate in this conversation.