Home > Software engineering >  How to validate max float digits laravel
How to validate max float digits laravel

Time:11-14

Hey guys I need to validate something on my laravel application and I dont know how.

So if the user introduces 1234.55 it should allow it, because it have only 5 numbers, but if the user introduces 12345678.55 must reject! What I have until now.

return [
    'max_debit' => 'required|numeric|min:0'
];

I tryed to use digits_between, but when I use this, the validation doesn't allow float numbers.

So the rule should match:

Integer numbers, float numbers -> All greater or equal to 0

Max digits -> 9

CodePudding user response:

You can do it with the regex rule: https://laravel.com/docs/master/validation#rule-regex

'max_debit' => [
    'required',
    'max:9',
    'regex:/^(([0-9]*)(\.([0-9] ))?)$/',
],

With the max you define a maximum of 9 characters. If you want to limit the digits after the period, you can add the {0,2} group where 0 stands for zero digits and 2 for max 2 digits after the period:

/^(([0-9]*)(\.([0-9]{0,2} ))?)$/

Answer based on this answer: https://stackoverflow.com/a/23059703/6385459

Is this what you're looking for?

CodePudding user response:

You Can use max:9 (max numeric value) or use also custom role in laravel

  • Related