form_number()
function form_number(string $name, string|int|float|null $value = null, array $attributes = []): string
Description
Generates an HTML number input field with type="number". Provides built-in browser number validation and step controls.
Parameters
| Parameter | Type | Description |
|---|---|---|
| $name | string | The name attribute for the input element. |
| $value | string|int|float|null | (optional) The numeric value for the input. |
| $attributes | array | (optional) HTML attributes for the input element. Defaults to an empty array ([]). |
Return Value
| Type | Description |
|---|---|
| string | An HTML number input element. |
Example #1: Basic Number Input
Example #2: Number Input with Range and Step
Example #3: Decimal Number Input
Number Input Attributes Explained
Number inputs support specialized attributes that control validation and user interaction:
min and max
The min and max attributes define the allowable numeric range. Browsers prevent values outside this range:
step
The step attribute controls the increment/decrement values when using arrow keys or spinner buttons:
The default step value is 1, which restricts input to whole numbers.
- Use
minandmaxattributes to restrict number ranges for better user experience. - For currency values, use
step="0.01"to enforce two decimal places. - Use
post('field_name', true, true)to automatically cast submitted values to integers or floats. - Always validate numbers server-side with PHP's
filter_var($value, FILTER_VALIDATE_FLOAT)oris_numeric(). - Remember that browser validation varies - some users may submit values outside allowed ranges.
- Number inputs trigger numeric keyboards on mobile devices, improving data entry.