validating a numeric input's length in laravel 5

PhpLaravelLaravel 5Laravel 5.2Laravel Validation

Php Problem Overview


foo.blade.php

<input type="text" name="national-id" />

FooController.php

$rules = [
    'national-id' => 'required|size:10|numeric'
];

the national-id field should contain 10 digits and I actually expected the code above to validate this , but instead It will check If the national-id exactly equals to 10 or not ...

how can I validate the length of a numeric field?

Php Solutions


Solution 1 - Php

In fact you don't need to use digits_between rule. You can use digits rule so according to documentation it will be enough to use:

$rules = [
    'national-id' => 'required|digits:10'
];

without numeric rule because digits rule also verify if given value is numeric.

Solution 2 - Php

When using size on a number it will check if the number is equal to the size, on string, it will check the amount of characters, for number you should use :

'number' => 'required|numeric|digits_between:1,10'

Solution 3 - Php

In laravel the size validation on a field that's numeric will indicate the max integer it cannot be larger than.

Use digits_between:

   'national-id' => 'required|digits_between:10,10|numeric' 

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
QuestionbobDView Question on Stackoverflow
Solution 1 - PhpMarcin NabiałekView Answer on Stackoverflow
Solution 2 - PhpCarlosView Answer on Stackoverflow
Solution 3 - PhpRayView Answer on Stackoverflow