Allow only numeric and float values with validation in Laravel

Issue

I have a field days in my model, now I want to only save integer or float values in this column.

Values can be like:

  • 1
  • 2
  • 0.5
  • 2.5

I tried to use numeric but then it is not allowing me to input float values:

$this->validate($request, [
    'days' => 'required|numeric|min:1|max:50',
]);

Any help is highly appreciated.

Thanks

Solution

You can try custom validation rules for your case.
Example:

'days' => 'required|regex:/^\d*(\.\d{2})?$/'

Answered By – Wadud Rahman

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published