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

shanely's avatar

How can I validate amount

Hi,

I have amount field in table and the datatype is decimal(12,2), how can I validate the amount that the user will not go beyond the size of my field.

I have this but it's wrong



$request->validate([
            'amount' => 'required|digits_between:1,99999999999999',
           
        ]);

Thank you

0 likes
10 replies
STEREOH's avatar

digits_between accepts a number of decimals.

so in your case it would be : digits_between:1,12

rodrigo.pedra's avatar

Have you tried:

$request->validate([
    'amount' => 'required|numeric|between:1,99999999999999',
]);

When using numeric or integer the between rule will validate against numeric values.

See here:

https://laravel.com/docs/6.x/validation#rule-between

The field under validation must have a size between the given min and max. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.

It uses the assumption from the size rule:

https://laravel.com/docs/6.x/validation#rule-size

shanely's avatar

It can't validate more than the size of the database field throws an exception

SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'amount' at row 1 (SQL: insert into `debits` (`amount`, `particulars`, `debit_date`, `created_by`, `updated_at`, `created_at`) values (999999999999.50, fdsfdsf, 2020-03-04, 1, 2020-03-06 14:03:42, 2020-03-06 14:03:42))
rodrigo.pedra's avatar

Haven't seen @stereoh answer before posting mine, so this seems a better approach:

$request->validate([
    'amount' => 'required|numeric|digits_between:1,12',
]);

Also if only non-negative amounts are allowed you can try adding:

$request->validate([
    'amount' => 'required|numeric|min:0|digits_between:1,12',
]);

Considering that digits_between expects a digit-only string, if you need to validate the decimal part you could use a custom validation rule:

$request->validate([
    'amount' => ['required', 'numeric', 'min:0', 
        function ($attribute, $value, $fail) {
            $value = floatval($value);

            if ($value < 0 || $value >= 1e12) {
                $fail($attribute . ' is invalid');
            }
        } ],
]);

Where 1e12 is a scientific notation for 1,000,000,000,000.00

You can read about custom closure validators here:

https://laravel.com/docs/6.x/validation#using-closures

shanely's avatar

I tried @stereoh,

if I input this 9999999999.12

my form will validate The amount must be between 1 and 12 digits. even that amount is accepted.

rodrigo.pedra's avatar

Which validations rules are you using?

In the last snippet from my last post I removed the digits_between rule as it ignores the decimal separator, see the comment I did there:

Considering that digits_between expects a digit-only string, if you need to validate the decimal part you could use a custom validation rule:

STEREOH's avatar
STEREOH
Best Answer
Level 18

Either use @bugsysha 's option

or

'required|numeric|between:-9999999999.99,9999999999.99'

or if you want only positives

'required|numeric|between:0,9999999999.99'

Please or to participate in this conversation.