Skip to content
Advertisement

Allow only numeric and float values with validation in Laravel

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

Advertisement

Answer

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

'days' => 'required|regex:/^d*(.d{2})?$/'
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement