Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validating a numeric input's length in laravel 5

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?

like image 707
bobD Avatar asked Sep 16 '16 20:09

bobD


People also ask

How do I validate a timestamp in laravel?

Just create a new validation rule in laravel to validate the timestamp... Validator::extend('isTimeStamp', function($attribute, $value, $parameters) { return ((string) (int) $value === $value) && ($value <= PHP_INT_MAX) && ($value >= ~PHP_INT_MAX); }); You can now use isTimeStamp validation rule to validate timestamp.

How do I validate an array in laravel?

you can call validate() method directly on Request object like so: $data = $request->validate([ "name" => "required|array|min:3", "name. *" => "required|string|distinct|min:3", ]);

How can I verify my phone number in laravel?

Validate Phone Number Laravel public function save(Request $request) { $validated = $request->validate([ phone_number=> 'required|numeric|min:10' ]); //If number passes validation, method will continue here. } In our save function we use the validate method provided by the Illuminate\Http\Request object.


2 Answers

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.

like image 179
Marcin Nabiałek Avatar answered Sep 20 '22 15:09

Marcin Nabiałek


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' 
like image 37
Carlos Avatar answered Sep 19 '22 15:09

Carlos