51

Laravel does not have a validation for decimal, so I need a regex or other validation method to validate a numeric value of 0 - 99.99

I have tried

required|regex:^\d{0,2}(\.\d{1,2})?$/
required|regex:[1-9]\d?(\.\d\d?)?|0\.[1-9]\d?||0\.0[1-9]
required|regex:/[1-9]\d?(\.\d\d?)?|0\.[1-9]\d?||0\.0[1-9]/
required|regex:/[\d]{2}.[\d]{2}/
required|regex:/[\d]{0-2}.[\d]{0,2}/

I am either getting "invalid format" when trying to enter 0.05 or unknown modifiers for preg_match

any help with the regex needed to validate a decimal in laravel is appreciated (with optional values before or after decimal

lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
Kender
  • 1,191
  • 3
  • 15
  • 34

6 Answers6

129

The laravel(5?) between rule does always get it has to parse a decimal, you have to add numeric to it to validate correctly

'required|numeric|between:0,99.99',
grisgruis
  • 1,389
  • 1
  • 8
  • 6
  • This fails on 0.5 – zeros-and-ones Feb 26 '17 at 19:35
  • 1
    late reply but you can try `'required|numeric|between:0.00,99.99',` – josevoid Oct 10 '17 at 09:58
  • 1
    What actually got this working for me was that my AJAX request was a string & in order to validate properly as numeric, I had to cast it as a float prior validation: `$request->merge(['inputName' => (float) $request->input('inputName')];` `required|numeric|0.01,9999.99` – cmeza Aug 24 '18 at 20:22
  • Beware: Based on the code of 'between': github.com/laravel/framework/blob/5.8/src/Illuminate/Validat‌​ion/… You aren't validating actual number of decimal positions, only that the number is between two real values: The 0 and the 9999.9900000... If you try to validate 9999.981 it will be accepted (and probably rounded to 9999.98 after), but if you try with 9999.991 it will fail. – ecdani Aug 29 '19 at 12:09
99

The Laravel between validation rule is actually pretty powerful and can handle decimal values as well. So there's no need to use regex just do this:

'required|between:0,99.99'
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
16

This works for me:

 function rules(){'field' => ['required','numeric', 'min:1','max:99.99', 'regex:/^\d+(\.\d{1,2})?$/']}

And if you want to custom the message, then:

public function messages()
   {
       return [
        'field.regex' => 'custom your message'
      ];
   }
Mario Inostroza
  • 686
  • 6
  • 6
6

this will work for number allowing two decimal precision:

function rules(){

   return [
   'field_name'=> array('regex:/^(?:d*.d{1,2}|d+)$/','min:1','max:10')
        ]; 
}
Neeraj Negi
  • 704
  • 8
  • 5
1

this will work for numbers with decimal and without decimals:

required|gte:0|lte:99

-4
 validate()->make([
            'min_commission' => 'required_with:end_page|numeric|min:0|max:0',
            'max_commission' => 'required_with:end_page|numeric|min:0|max:99',

        ]);
sirisha
  • 1
  • 1
  • 2
    There are five existing answers to this question, including a top-rated answer with **98 upvotes**. Are you sure your answer hasn't already been provided? If not, why might someone prefer your approach over the existing approaches proposed? Are you taking advantage of new capabilities? Are there scenarios where your approach is better suited? Explanations are _always_ useful, but are _especially_ important here. – Jeremy Caney Dec 02 '21 at 00:12