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

catalyph's avatar

Validation of numeric max_digits interfere with decimals

Having trouble with this validation and decimal numbers

request()->validate([
     'hours'=>['numeric','max:24.00','max_digits:4'],     
 ]);

if the user enters 7.5 the validation fails with:

The hours field must not have more than 4 digits.

Clearly 7.5 does not have more than 4 digits and the dd() indicates

 "hours" => "7.5"
0 likes
7 replies
Snapey's avatar

max digits rule says

The integer under validation must have a maximum length of value.

The INTEGER. 7.5 is not an integer.

dha1095's avatar

You can use regex for this

request()->validate([ 'hours' => ['numeric', 'regex:/^\d{1,2}(.\d{1,2})?$/', function($attribute, $value, $fail) { if ($value > 24.00) { $fail('The ' . $attribute . ' field must be less than or equal to 24.00.'); } }], ]);

1 like
catalyph's avatar

@dha1095 COOL! I went with the simple answer from @dhpandya, but I will keep this in the back pocket for future, it may be helpful!

1 like
Snapey's avatar

The premise is wrong. What is 7.5?

What if the user types 07:30 or 0730 or 16.21?

Provide a proper time picker so the user knows what they are supposed to enter.

catalyph's avatar

@Snapey you gathered that the premise is wrong based off of my very simple demo? Validate as decimal exactly worked for my needs. Im handling other entries differently.

What would you propose as a proper time picker. I came for a solution, not just to be told what I'm doing is incorrect, neither of your inputs were helpful or provided any solution.

Snapey's avatar

@catalyph I don't know your use case to offer any solution. I'm only guessing that you are asking the user to enter a duration of up to a day? I can't really say without knowing in what circumstance the user might enter 7.5 or 24

Please or to participate in this conversation.