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

ParameterTypeDescription
$namestringThe name attribute for the input element.
$valuestring|int|float|null(optional) The numeric value for the input.
$attributesarray(optional) HTML attributes for the input element. Defaults to an empty array ([]).

Return Value

TypeDescription
stringAn 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 min and max attributes 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) or is_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.