Skip to content
Sveltier
Mode
Forms

NumberField

A number typed in the reader's own format, stepped with the keyboard or two buttons, and bound and submitted as a plain number.

Usage

Bind the value: a number, or null when the field is empty. The user types the number as their locale writes it, such as 1,234.5 or 1.234,5; when they leave the field it is written with the locale's grouping and the digits of format.

From 1 to 99

USD

Try 1234.5, then leave the field

Bound values: quantity = 1, amount = null

Order.svelte
<script>
  import { NumberField } from 'sveltier';
  let quantity = $state(1);
  let amount = $state(null);
</script>

<NumberField label="Quantity" name="quantity" min={1} max={99} step={1} bind:value={quantity} />

<!-- Digits only in format: the currency goes in prepend, so the text stays editable. -->
<NumberField
  label="Amount"
  name="amount"
  min={0}
  format={{ minimumFractionDigits: 2, maximumFractionDigits: 2 }}
  required
  bind:value={amount}
>
  {#snippet prepend()}USD{/snippet}
</NumberField>

Range and steps

min and max stop the keys and the buttons. A typed value outside them is not changed for the user: the field says what to enter instead. With step, the keys and buttons move along the steps counted from min, and a typed value between two steps is an error; without it they move by 1 and any number is accepted.

Format and currency

format takes Intl.NumberFormat options for the digits only: grouping and fraction digits. A currency or percent sign in the text would make it unreadable as a number once the user edits it, so put the currency or unit in prepend or append; in development, a currency or percent style warns.

In a form

With name, a hidden input submits the plain number, such as 1234.5, while the field shows 1,234.50. Validation works as on a TextField: required, rules (they receive the number), error and validateOn.

Accessibility

  • The input is a spinbutton with its value, range and the formatted text as aria-valuetext.
  • ↑ and ↓ move one step, Page Up and Page Down ten, Home and End go to min and max.
  • The decrease and increase buttons are named, stay out of the tab order (the keys do the same), keep focus in the field, and are at least 24 px square.
  • Phones show a number keypad: a numeric one for whole numbers of zero or more, a decimal one otherwise.

API

NameTypeDefaultDescription
variant'outlined' | 'tonal' | 'elevated' | 'text' | 'plain''outlined'How the box is drawn.
colorColor'primary'The accent of the focused field. An invalid field uses error.
size'xs' | 'sm' | 'md' | 'lg' | 'xl''md'Height and text size, matching a Button of the same size.
density'compact' | 'default' | 'comfortable''default'Multiplies the height and padding, never the text.
roundedboolean | 'none' | 'sm' | 'md' | 'lg' | 'xl' | 'full'trueCorner rounding. true keeps the theme's radius.
disabledbooleanfalseMutes the field and disables its control.
labelstring–The visible label, linked to the control. Without it, name the control with aria-label.
hintstring–Help under the field. An error replaces it.
errorFieldError–An error from outside the field, such as the server's answer: a message, a list of which the first is shown, or true for an invalid state described elsewhere.
prependSnippet–Content at the start of the box, usually an icon.
appendSnippet–Content at the end of the box: an icon, a unit or a button.
valuenumber | null–The number, or null when the field is empty. Bindable.
minnumber–The smallest value. The keys and buttons stop here; a smaller typed value is an error.
maxnumber–The largest value. The keys and buttons stop here; a larger typed value is an error.
stepnumber–How far the arrow keys and the buttons move; Page Up and Page Down move ten steps. When set, a typed value off the steps, counted from min, is an error. When not set, they move by 1 and any number is accepted, decimals included.
formatIntl.NumberFormatOptions{ maximumFractionDigits: 20 }How the number is written once the user leaves the field, as Intl.NumberFormat options in the app's locale: digits only, such as { minimumFractionDigits: 2 }, so the user can edit what they see. Put a currency or a unit in prepend or append.
controlsbooleantrueShows decrease and increase buttons at the end of the field.
rulesreadonly Rule<number>[]–Checks run in order after the HTML constraints, with the number. The first message is shown.
validateOn'blur' | 'input' | 'submit'–When the message shows. Defaults to the form's setting, else 'blur'.
requiredbooleanfalseThe field must not be empty. Adds an asterisk to the label.
readonlybooleanfalseThe number can be read and copied but not changed.
namestring–Submits the plain number, such as 1234.5, under this name in a form.
stylestring | null–Inline style of the field's root element.
classClassValue | null–Classes added after the component's own: a string, an array or an object.
refHTMLInputElement | null–The root element, bindable with bind:ref.