Laravel set between digits in validation
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Introduction
The Laravel framework provides robust validation functionality for handling user inputs. However, sometimes the built-in rules may not serve your specific requirements. In this blog post, we will discuss how to implement custom validation rules using Laravel 5.x when checking for input between digits (integers) between 1 and 10.Leveraging Built-In Validation Rules
Before we dive into creating our own validation rule, let us first examine the given example code.php
'required|integer|digits_between:1,10'Creating Custom Validation Rules
In Laravel, we can create custom validation rules by extending the 'Validator' class in the AppServiceProvider or in a separate service provider file. Below is an example of creating a new validator for checking digits between 1 and 10:php
class DigitBetweenOneAndTenValidator extends Validator
{
/**
* Create a new custom validation rule instance.
*
* @param mixed $value The value to validate.
* @return bool|string
*/
public function __construct($value)
{
parent::__construct();
// Assign the given value and range values.
$this->value = $value;
$this->min = 1;
$this->max = 10;
}
/**
* Define the rule validation logic.
*
* @param string $attribute
* @return bool|string
*/
public function validate($attribute, $value)
{
// Check if the value is an integer and falls within the range of 1 to 10.
if (!is_int($this->value) || $this->min > $this->value || $this->max < $this->value) {
return "The input must be a digit between 1 and 10.";
}
// If the validation passes, return an empty string.
return '';
}
}Implementing the Custom Rule
To use our newly created custom rule, we can now modify the original code example to call the appropriate validator method.php
'display_post_count' => 'required|min:1|max:10',php
'display_post_count' => 'required|digitBetweenOneAndTen',