Laravel Rule Validation for Numbers

PhpLaravelLaravel 4

Php Problem Overview


I have the following Rule :

'Fno' => 'digits:10'
'Lno' => 'min:2|max5'  // this seems invalid

But How to have the Rule that

Fno Should be a Digit with Minimum 2 Digit to Maximum 5 Digit and

Lno Should be a Digit only with Min 2 Digit

Php Solutions


Solution 1 - Php

If I correctly got what you want:

$rules = ['Fno' => 'digits_between:2,5', 'Lno' => 'numeric|min:2'];

or

$rules = ['Fno' => 'numeric|min:2|max:5', 'Lno' => 'numeric|min:2'];

For all the available rules: http://laravel.com/docs/4.2/validation#available-validation-rules

> digits_between :min,max > > The field under validation must have a length between the given min > and max. > > numeric > > The field under validation must have a numeric value. > > max:value > > The field under validation must be less than or equal to a maximum > value. Strings, numerics, and files are evaluated in the same fashion > as the size rule. > > min:value > > The field under validation must have a minimum value. Strings, > numerics, and files are evaluated in the same fashion as the size > rule.

Solution 2 - Php

$this->validate($request,[
        'input_field_name'=>'digits_between:2,5',
       ]);

Try this it will be work

Solution 3 - Php

The complete code to write validation for min and max is below:

    $request->validate([
'Fno' => 'integer|digits_between:2,5',
'Lno' => 'min:2',

]);

Solution 4 - Php

Also, there was just a typo in your original post.

'min:2|max5' should have been 'min:2|max:5'.
Notice the ":" for the "max" rule.

Solution 5 - Php

Laravel min and max validation do not work properly with a numeric rule validation. Instead of numeric, min and max, Laravel provided a rule digits_between.

$this->validate($request,[
        'field_name'=>'digits_between:2,5',
       ]);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAngularAngularAngularView Question on Stackoverflow
Solution 1 - PhpDamien PirsyView Answer on Stackoverflow
Solution 2 - PhpRipon UddinView Answer on Stackoverflow
Solution 3 - PhpSumit SinghView Answer on Stackoverflow
Solution 4 - PhpPeter KrebsView Answer on Stackoverflow
Solution 5 - PhpSumit Kumar GuptaView Answer on Stackoverflow