Laravel rule validation for numbers

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Comprehensive Guide to Laravel Rule Validation for Numbers Introduction Laravel's request validation system provides an easy way to validate user input and ensure data integrity for your application. While the provided rules offer basic functionality, sometimes you need more complex rules that involve numbers and specific ranges. In this article, we will explore how to define various number-specific rules in Laravel, including digits count range rules for a field, digit-only rules with minimum length requirement, and best practices for working with these validation rules in your application. Let's break down the initial example provided: `Fno => 'digits:10', // This rule seems to be incorrectly defined` `Lno => 'min:2|max:5', // This rule seems invalid but could represent a digit-only field with a minimum length of 2 digits, though the max range is uncertain` Let's tackle each problem step by step. We will create accurate and practical validation rules for both fields while incorporating relevant best practices. Fno Rule - Digits Count Range (10 Digits) Firstly, we want to implement a rule that checks the Fno field to have only digits with a minimum of 2 digits and a maximum of 5 digits. The validation should be as follows: - Field must be numeric (digits) - Minimum length is 2 digits - Maximum length is 5 digits To achieve this, we can use Laravel's built-in rule sets and custom rules. Here's how the validation could look like: ```php $request->validate([ 'Fno' => [ 'required', // rule set for digits count range Rule::custom(function ($attribute, $value) { return preg_match('/^\d{2,5}$/', $value); }), ], ]); ``` With this code, the validation function will perform three checks: - The field Fno is required. - The value must be a number with a minimum length of 2 digits and a maximum length of 5 digits using a regular expression (preg_match) to check for valid digits within that range. Note that we also use the "required" rule set since we want Fno to be mandatory, but if this wasn't the case, you could simply remove this line. Lno Rule - Digit-Only Field with Minimum Length of 2 Digits For the Lno field, we just need a rule that ensures its value contains only digits and has a minimum length of 2 digits. We can write it as follows: ```php $request->validate([ 'Lno' => [ // rule set for digit-only fields with min length of 2 digits Rule::custom(function ($attribute, $value) { return preg_match('/^\d{2,}$/', $value); }), ], ]); ``` In this case, the validation checks if Lno contains only numbers and has a length of at least 2 digits. We can further adjust the regular expression to accommodate other scenarios (e.g., min:3 for three or more digits). Best Practices for Laravel Rule Validation When working with number-specific rules in your application, always follow these best practices: 1. Keep validation rules as simple and clear as possible. Use built-in rule sets whenever applicable. 2. Test all validation scenarios to ensure accurate results. 3. Document the different rules and their behaviors within the code or project documentation for easy reference. 4. If you need more complex logic, create custom validation functions like we did above to handle specific cases. 5. Always validate inputs before processing them in your application. This ensures data integrity and protects against potential vulnerabilities. 6. Follow Laravel's guidelines on how to define request validation rules for various types of fields (e.g., email, dates, etc.). Conclusion By following these guidelines, you can effectively create accurate number-specific rules in your Laravel application using the request validation system. Always aim for clear and well-defined rules with proper documentation to ensure efficient collaboration within your team and maintain clean code. Remember that Laravel provides a versatile validation system allowing for various scenarios, so don't hesitate to explore and utilize its full potential to meet your project's needs.