Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

data-accept

Character filter for text inputs: block characters outside a preset or custom character class while the user formats freely. Presets for phone, date, card numbers, letters and more.

Overview

data-accept is a character filter for text inputs. Characters outside the allowed set are blocked as they are typed or pasted; everything else, including the order and the separators, is up to the user. Use data-mask instead when the value must have one fixed shape.

It works on text-like <input> types (text, tel, search and similar). It does not filter files: to limit a file picker, use the native accept attribute on <input type="file"> (see data-upload).

Usage

Add a preset name or a bracketed character class. Say what is allowed in visible help text linked with aria-describedby, so people who miss a blocked keystroke know why.

Options

AttributeValuesDefaultEffect
data-acceptpreset name, or a character class starting with [—Allowed characters. An unknown preset or an invalid class logs a warning and leaves the input unfiltered.
data-accept-pattern(set by the script)—The regex source in use, for your own scripts. Read-only.
inputmodenativefrom the presetSet from the preset (table below) unless you set it yourself.

Presets

PresetAllowed charactersinputmode
digits0-9numeric
alphaa-z A-Z (ASCII only: rejects é, ß, Ж)text
lettersletters and combining marks in any script (\p{L}\p{M})text
alphanuma-z A-Z 0-9 (ASCII only)text
phonedigits, + ( ) - and spacetel
datedigits, / - .numeric
hex0-9 a-f A-Ftext
currencydigits, . ,decimal
ccdigits, space, -numeric

A custom class is compiled as a JavaScript RegExp in Unicode mode, so [\p{L}\p{N}] and characters outside the Basic Multilingual Plane (emoji, rare CJK) work. Each character is tested on its own.

Behaviour

  • Typing — an invalid character is blocked in beforeinput, so it never appears.
  • Paste — invalid characters are removed and the rest is inserted with execCommand('insertText'), which keeps undo working.
  • IME — filtering waits for compositionend, then strips anything invalid.
  • Fallback — where beforeinput cannot be cancelled, the input handler strips invalid characters and keeps the cursor after the kept text.
  • Prefilled values and reset — the starting value is filtered when the input is enhanced, and again after a form reset restores the default.
  • With data-mask — if both are on one input, data-mask wins and a console warning is logged.

data-accept adds no constraint validation. It sets no pattern, values set from script are not filtered, and nothing stops a request built by hand. Add pattern, minlength or required for the shape you need, and validate on the server.

Events

data-accept fires no events of its own. The native input event fires for accepted text as usual; a blocked keystroke fires nothing. When the paste fallback writes the value directly, it dispatches a bubbling input event so listeners still see the change.

Styling

No public hooks. data-accept only filters characters; the input keeps its normal input styles.

Accessibility

  • Blocked characters give no feedback, so describe the allowed characters in visible help text linked with aria-describedby (an <output class="hint"> in <form-field>). A placeholder is not a reliable description: it disappears on input and is often not announced.
  • inputmode from the preset brings up a fitting on-screen keyboard.
  • IME composition is never interrupted.
  • Use letters, not alpha, for names: alpha rejects letters outside A–Z.
  • Without JavaScript the input accepts any text.

Examples

Custom character class with a pattern

The filter keeps out other characters; pattern makes the form require exactly six.

All presets, paste and data-mask side by side

data-accept or data-mask?

data-acceptdata-mask
SeparatorsTyped by the userInserted for the user
FormatAny order of allowed charactersOne fixed shape
Use whenSeveral formats are validYou define the format

Related

  • data-mask — fixed-format input with inserted separators. Choose it when there is exactly one valid shape.
  • <input> — native pattern, inputmode and, for files, accept.
  • <form-field> — label, hint and validation messages around the input.
  • data-upload — file drop zone; file types are limited by the native accept attribute, not data-accept.
  • accept — the native file-type attribute on file inputs, which is unrelated to data-accept.
  • inputmode — choose the keyboard to match the characters you allow.
↑