Mask Input
A text input that fits what is typed to a pattern, for postcodes, IBANs, tax IDs, expiry dates and other fixed formats.
c2-mask-input is a text field for values with a fixed shape. The mask says what each position accepts: 9 a digit, a a letter, A a letter in upper case, * a letter or digit and X a letter or digit in upper case. Any other character is a literal the field inserts itself, \ turns a mask character into a literal (\9), ? makes the rest of the mask optional, for values whose length varies, and [ ] wrap an optional group anywhere in the mask, filled or skipped as a whole: A[A]9[X] 9AA takes every UK postcode, M1 1AE as well as SW1A 1AA, and the field picks the groups to skip from what is typed, so m11ae becomes M1 1AE. Groups do not nest; write \[ and \] for a literal bracket. From script, tokens adds mask characters of your own, such as { H: { pattern: /^[0-9a-f]$/i } } for hex digits.
Characters a position does not accept are dropped, so the user can type with or without the separators, paste a formatted or unformatted value, or backspace across a separator, which deletes the character before it. While the field has focus or a value, the rest of the mask shows behind the text in slot-chars (_ by default); placeholder shows while it is empty and blurred. A mask of digits only brings up the numeric keyboard (inputmode overrides it).
value holds the typed characters only, 1226 for 12/26; with include-literals it holds the formatted text. The element is form-associated (name) and submits that same value. It validates like an input: required checks for a value, and an incomplete one is invalid. complete fires when the last required position is filled, and the host matches :state(complete). formatMask and unmask are exported for formatting a value outside the field.
Import
import '@c2n/components/mask-input'