Skip to content

Validation

An isAllowed predicate rejects edits the user makes (typing, deleting, paste, drop, autofill, IME) before they commit. Use it for business rules or format constraints that don't fit a built-in option.

setValue('500')

A value set from code is never checked

type IsAllowed = (rawValue: string) => boolean

rawValue is the sanitized, raw (no-separator) string that would be committed. Return true to accept it, false to discard it. The input keeps its previous value and no onValueChange fires.

Example: cap at 100

import { NumoraInput } from 'numora'

const input = new NumoraInput(container, {
  isAllowed: (raw) => {
    // Half-typed values. "-" and "-." only arrive with allowNegative: true
    if (raw === '' || raw === '-' || raw === '.' || raw === '-.') return true
    const n = parseFloat(raw)
    return Number.isFinite(n) && n <= 100
  },
})

Example: whole numbers, up to 6 digits

new NumoraInput(container, {
  isAllowed: (raw) => /^\d{0,6}$/.test(raw),
})

Notes

  • Runs after sanitization and formatting. For typing and paste that is before the DOM is mutated. For drop, autofill and IME text, which the browser has already inserted, a rejection puts the previous value back
  • Fires for typing, deletion, paste, drop, autofill and IME text. A keystroke that changes nothing, or one a built-in option already refused, never reaches it
  • Does not run for values your code writes: defaultValue, setValue() and update() are never checked
  • Does not run when blur settles the value. If blur turns 1. into 1 or pads 1.5 to 1.50, the settled value is not checked
  • A predicate that throws counts as a rejection, and the error is logged with console.error
  • Always allow the intermediate strings users need to type their way to a valid value (empty string, lone - or decimal separator)
  • Keep the predicate cheap, because it runs on every edit
  • For length and decimal-precision caps, use max length and decimals instead

Invalid options

Numora checks every option in the constructor and in update(). An invalid one throws an Error. A failed update() leaves the field with its old options.

OptionInvalid whenError message
maxLengthNot a whole number of 1 or moremaxLength must be a positive integer (>= 1). Received: 0
decimalMaxLengthNot a whole number of 0 or moredecimalMaxLength must be a non-negative integer. Received: -1
decimalMinLengthNot a whole number of 0 or moredecimalMinLength must be a non-negative integer. Received: -1
decimalMinLengthGreater than decimalMaxLengthdecimalMinLength (3) cannot be greater than decimalMaxLength (2).
formatOnNot 'blur' or 'change'formatOn must be one of: 'blur', 'change'. Received: "input"
groupingStyleNot one of the four stylesgroupingStyle must be one of: 'none', 'thousand', 'lakh', 'wan'. Received: "indian"
thousandSeparatorAn empty stringthousandSeparator cannot be empty. Use groupingStyle: 'none'.
thousandSeparator, decimalSeparatorLonger than one character. For decimalSeparator, also an empty stringdecimalSeparator must be a single character. Received: ", "
thousandSeparator, decimalSeparatorA digitthousandSeparator cannot be a digit. Received: "1"
thousandSeparator, decimalSeparatorOne of - + e E k K m M b B t TthousandSeparator cannot be one of the reserved characters -+eEkKmMbBtT (sign, exponent, or compact-notation suffix). Received: "k"
thousandSeparator, decimalSeparatorBoth passed, as the same characterdecimalSeparator cannot equal thousandSeparator. Both were ".".
allowNegative, allowLeadingZeros, allowCompactNotation, allowScientificNotation, padLeadingZeroNot a booleanallowNegative must be a boolean. Received: "yes"
isAllowed, onValueChange, onFormattedValueChangeNot a functionisAllowed must be a function. Received: "x"
localeA string that is not a BCP 47 taglocale must be a BCP 47 tag such as "de-DE". Received: "not a tag"

These never throw:

  • One separator passed alone. If it lands on the other separator, the other one switches between . and ,.
  • A well-formed locale tag the runtime does not know. The runtime picks its closest match.
  • A removed v4 option such as thousandStyle or maxDecimals. Numora ignores it and, in development, warns with the name of its replacement. The migration guide lists every one.