Skip to content

Max length

Max length caps the input by raw character count, so thousand separators are excluded. The native maxLength attribute counts formatted characters: 1,234,567 is 9 characters but only 7 digits, and Numora counts 7.

maxLength: 4

A paste is cut to 4 digits

import { NumoraInput } from 'numora'

const input = new NumoraInput(container, {
  maxLength: 7,
})

// User types: 1, 2, 3, 4, 5, 6, 7  → "1,234,567"  (allowed)
// Next keystroke is blocked - raw length is already 7.

Rules

  • Counts the raw value after sanitization. Thousand separators and decimalMinLength padding are excluded
  • The decimal separator is counted, because it is part of the raw value
  • A leading - sign is counted
  • Numora truncates from the right. If that would leave a trailing decimal separator, it removes the separator too ("1234.56" capped at 5 → "1234")
  • If truncation would leave a bare "-", the result is ""
  • The cut comes before the decimals are trimmed to decimalMaxLength. With maxLength: 4, a pasted 1.23456789 becomes 1.23, a pasted 123456 becomes 1234, and a pasted 123.456 becomes 123
  • maxLength must be a whole number of 1 or more. See invalid options

Pasted values

Numora truncates a paste after sanitizing it. Pasting "99999999" into an input with maxLength: 4 commits "9999".

Typing is different. A keystroke that would take the raw value past the limit is rejected, and nothing is cut. Values your code passes in are truncated like a paste.

Truncation happens after compact and scientific notation expand, so it can change the magnitude. With allowCompactNotation and maxLength: 5, a pasted 1.5m expands to 1500000 and is cut to 15000. To refuse an over-long paste, drop maxLength and check the length in isAllowed. With both set, isAllowed only sees the value after it was cut.

Standalone utility

To truncate a string outside the input pipeline, import truncateToMaxLength from the numora/pipeline entry point:

import { truncateToMaxLength } from 'numora/pipeline'

truncateToMaxLength('1234567', 5)        // → '12345'
truncateToMaxLength('1234,56', 5, ',')   // → '1234'   (custom separator)