XGitHub

Max Length

Cap the input by raw character count - thousand separators are excluded from the limit, so the cap reflects the actual numeric value rather than the formatted display.

Why not the native HTML attribute?

The native maxLength attribute on <input> counts every formatted character. With thousand separators, 1,234,567 is 9 characters; the user thinks of it as 7 digits. Numora's option counts the raw value the way users actually reason about length.

import { NumoraInput } from 'numora'

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

// 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 - thousand separators are stripped before measuring
  • The decimal separator is counted (it's part of the raw value)
  • A leading - sign is counted
  • Truncation happens from the right; if it would leave a trailing decimal separator, that separator is also stripped ("1234.56" capped at 5 → "1234")
  • If truncation would leave a bare "-", the result is ""

Pasted values

Pastes are truncated after sanitization. Pasting "99999999" into an input with maxLength: 4 commits "9999".

Standalone utility

For one-off truncation outside of the input pipeline, Numora exportstruncateToMaxLength:

import { truncateToMaxLength } from 'numora'

truncateToMaxLength('1234567', 5)        // → '12345'
truncateToMaxLength('1234.56', 5)        // → '1234'   (trailing dot stripped)
truncateToMaxLength('-1234567', 5)       // → '-1234'  (minus counts)
truncateToMaxLength('1234,56', 5, ',')   // → '1234'   (custom separator)