Laravel rule validation for numbers

I have the following rules :

'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 digits to maximum 5 digits and

Lno should be a digit only with min 2 digits

279862 次浏览

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.

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.

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

Try this it will be work

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',
]);

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

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


]);

In addition to other answers, i have tried them but not worked properly. Below code works as you wish.

$validator = Validator::make($input, [
'oauthLevel' => ['required', 'integer', 'max:3', 'min:1']
]);