validating a numeric input's length in laravel 5
Asked Answered
H

3

64

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?

Hanford answered 16/9, 2016 at 20:13 Comment(0)
H
130

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.

Husky answered 16/9, 2016 at 20:45 Comment(1)
sidenote: digits:10 means: "must be exactly 10 digits" (not less not more)Toandfro
G
56

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'
Garman answered 16/9, 2016 at 20:21 Comment(1)
Please note, that if your number contains fractional part it will be automatically invalidated, you can use only integersRoomful
T
17

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' 
Transformism answered 16/9, 2016 at 20:17 Comment(3)
Is It the only way? I thought there must be rule like length or etc to figure this outHanford
work's well but when the user enter's a wrong value , the error Is this (The national-id must be between 10 and 10 digits)!Hanford
you don't need to use numericBendicty

© 2022 - 2024 — McMap. All rights reserved.