AngularAngularAngular
AngularAngularAngular

Reputation: 3769

Laravel rule validation for numbers

I have the following rules :

'Fno' => 'digits:10'
'Lno' => 'min:2|max:5'  // 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

Upvotes: 93

Views: 378463

Answers (7)

almiros
almiros

Reputation: 151

The first parameter in the "Lno" rule is the type of the rule (digits), the second parameter (between) is an option for the rule "digits" as it stood here. The option "between" must have 2 arguments for working properly, an "min " and an "max" argument. In that case 2 and 5. It means not less then 2 digits and not more as 5 digits allowed.

'Lno' => 'digits between:2,5' // (not less 2 and not more as 5 digits)

For more details look at: Laravel rule "between"

Upvotes: 0

bugracetin
bugracetin

Reputation: 167

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

Upvotes: 1

Sumit Singh
Sumit Singh

Reputation: 81

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

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

]);

Upvotes: 8

Sumit Kumar Gupta
Sumit Kumar Gupta

Reputation: 2364

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

Upvotes: 5

Ripon Uddin
Ripon Uddin

Reputation: 714

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

Try this it will be work

Upvotes: 11

Peter Pointer
Peter Pointer

Reputation: 4162

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.

Upvotes: 3

Damien Pirsy
Damien Pirsy

Reputation: 25435

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.

Upvotes: 158

Related Questions