📅  最后修改于: 2023-12-03 15:18:02.605000             🧑  作者: Mango
When building web applications with Laravel, it is common to need to validate user input. One type of validation that is often necessary is numeric validation. This ensures that the user entered only numeric values in a particular input field, such as a phone number or a credit card number.
Laravel provides a convenient way to perform numeric validation using the numeric
rule. Here's an example of how to use it:
$request->validate([
'phone_number' => 'required|numeric'
]);
In the above example, we're validating that the phone_number
field is required and contains only numeric values.
It's also possible to specify minimum and maximum values for the numeric input field. Here's an example:
$request->validate([
'age' => 'required|numeric|min:18|max:99'
]);
In this example, we're validating that the age
field is required, contains only numeric values, and is between 18 and 99.
If the validation fails, Laravel will throw a ValidationException
and redirect the user back to the previous page with the validation errors displayed.
Overall, the numeric
validation rule in Laravel makes it easy to ensure that your user input is only numeric, and to enforce minimum and maximum values for numeric fields.