Laravel provides the numeric validation rule for checking whether a value is numeric. However, there is an important difference between numeric and numeric:strict.
# The numeric Rule
The standard numeric rule accepts values that PHP considers numeric. This means both integers and numeric strings can pass validation.
$request->validate([
'age' => 'numeric'
]);
# these values can pass:
# 123
# '123'
# '12.5'
This is useful when your request data comes from sources such as HTML forms, where numbers are commonly received as strings.
# The numeric:strict Rule
numeric:strict is stricter about the value’s type. It requires the value to actually be an integer or a float rather than a numeric string.
$request->validate([
'age' => 'numeric:strict',
]);
# For example:
# 123 passes
# 12.5 passes
# '123' fails
# '12.5' fails
# Which One Should You Use?
Use numeric when numeric strings are acceptable, which is common with form and request input.
Use numeric:strict when you need the value to already be an actual integer or float and don’t want numeric strings to pass validation.
The key difference is simple: numeric checks whether the value is numeric, while numeric:strict also requires it to have a numeric PHP type.