validating a numeric input's length in laravel 5
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Validating Numeric Input's Length in Laravel 5
Body:
In web development, ensuring user input meets certain criteria is crucial for maintaining application integrity. Laravel's validation rules help us do just that. The provided code seems to have a small challenge which is validating the length of a numeric field. In this post, we will explore how to fix this issue and provide a more comprehensive solution for your form's validation needs.
Let's analyze the given code:
```php
$rules = [
'national-id' => 'required|size:10|numeric',
];
```
Here, we can see that there are three rules applied to the `national-id` field. Two of them validate specific criteria – one checks if the input is required and another ensures it consists only of numeric values. The third rule, `size:10`, appears to be in conflict with our real requirement as it's checking for an exact length of 10 characters.
To overcome this issue, we can make use of Laravel's custom validation rules. Let us alter the existing code to reflect the desired condition that the national ID field should contain exactly ten digits:
```php
$rules = [
'national-id' => [
'required',
'digits:10', // This validates a numeric input with 10 digits
'numeric'
],
];
```
In this code, we have added the `digits:10` validation rule to ensure that only ten digits are present in the national ID field. The first two rules (required and digits:10) are now chained together in an array, allowing for multiple validations on a single field.
Alternatively, you could also create a custom validation rule as below:
```php
/**
* A custom Laravel Validation Rule to check the length of a numeric input.
*
* @param \Illuminate\Validation\Validator $validator
* @return void
*/
Validator::extend('numericLength', function ($attribute, $value, $parameters) {
if (is_numeric($value)) {
return strlen((string)$value) === $parameters[0] ? true : false; // Validate numeric input with specified length
} else {
return false;
}
});
```
Now, you can call this custom validation rule in your controller like so:
```php
$rules = [
'national-id' => 'numeric_length:10', // validates a numeric input with specified length of 10 digits
];
```
This approach allows for even more flexibility and control over your form's validation logic.
To conclude, ensuring proper validation in Laravel 5 is critical for maintaining data integrity. By using the correct rules and customizing as needed, developers can easily implement complex validation scenarios like the one discussed here. Remember to always check our guide on Laravel Company's website for detailed information about Laravel Validation features.