Guide

Working examples

Twenty live form fields covering every format type, decorations, the regex catalog, check digits and the date tools, with copyable source files.

Each example is a real form field. The preview, the copied files and the Home demo share one scenario definition, so the options shown in code are the options running in the preview. Type into the field, try the sample values, then open the code.

Open the code under each example to copy its files. They run in two loading modes. HTML / CDN files work after saving them to one folder and opening index.html (network access to jsDelivr is required). Package / ESM files import @samline/formatter and need a bundler or dev server such as Vite.

Phones

Phone by country

format(value, 'phone', { country }) groups digits for the selected country through libphonenumber-js. Phones are opt-in: the copied ESM file calls registerPhone() from @samline/formatter/phone, and the CDN file loads formatter.phone.global.js; every other example uses the smaller core script. Changing the country reformats the current digits; the listener reads the country on every call instead of being re-attached.

PhoneLive · formatted and raw

A phone number for any country

format(value, 'phone', { country })

Type or paste a number. The selected country decides the grouping; the backend receives digits, plus a leading + for international numbers.

Shown to people
55 1234 5678
Sent to your backend
5512345678

Changing the country reformats the same digits. One input listener and one change listener read the current country, so listeners never pile up.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A phone number for any country</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="phone-country">Country</label>
        <select id="phone-country">
          <option value="MX" selected>Mexico</option>
          <option value="US">United States</option>
          <option value="ES">Spain</option>
          <option value="GB">United Kingdom</option>
          <option value="AR">Argentina</option>
          <option value="DE">Germany</option>
        </select>
        <label for="phone">Mobile phone</label>
        <input
          id="phone"
          type="text"
          inputmode="tel"
          autocomplete="tel"
          placeholder="55 1234 5678"
          value="5512345678"
          aria-describedby="phone-raw"
        />
        <input type="hidden" name="phone" />
        <p id="phone-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.phone.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format } = window.Formatter

const field = document.querySelector('#phone')
const hidden = document.querySelector('input[name="phone"]')
const rawOutput = document.querySelector('[data-raw]')
const country = document.querySelector('#phone-country')
const options = {}

const update = () => {
  const { formatted, raw } = format(field.value, 'phone', { ...options, country: country.value })
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
}

field.addEventListener('input', update)
country.addEventListener('change', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A phone number for any country</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="phone-country">Country</label>
        <select id="phone-country">
          <option value="MX" selected>Mexico</option>
          <option value="US">United States</option>
          <option value="ES">Spain</option>
          <option value="GB">United Kingdom</option>
          <option value="AR">Argentina</option>
          <option value="DE">Germany</option>
        </select>
        <label for="phone">Mobile phone</label>
        <input
          id="phone"
          type="text"
          inputmode="tel"
          autocomplete="tel"
          placeholder="55 1234 5678"
          value="5512345678"
          aria-describedby="phone-raw"
        />
        <input type="hidden" name="phone" />
        <p id="phone-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.phone.global.js"></script>
    <script>
      const { format } = window.Formatter

      const field = document.querySelector('#phone')
      const hidden = document.querySelector('input[name="phone"]')
      const rawOutput = document.querySelector('[data-raw]')
      const country = document.querySelector('#phone-country')
      const options = {}

      const update = () => {
        const { formatted, raw } = format(field.value, 'phone', {
          ...options,
          country: country.value,
        })
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
      }

      field.addEventListener('input', update)
      country.addEventListener('change', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A phone number for any country</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="phone-country">Country</label>
        <select id="phone-country">
          <option value="MX" selected>Mexico</option>
          <option value="US">United States</option>
          <option value="ES">Spain</option>
          <option value="GB">United Kingdom</option>
          <option value="AR">Argentina</option>
          <option value="DE">Germany</option>
        </select>
        <label for="phone">Mobile phone</label>
        <input
          id="phone"
          type="text"
          inputmode="tel"
          autocomplete="tel"
          placeholder="55 1234 5678"
          value="5512345678"
          aria-describedby="phone-raw"
        />
        <input type="hidden" name="phone" />
        <p id="phone-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format } from '@samline/formatter'
import { registerPhone } from '@samline/formatter/phone'

registerPhone()

const field = document.querySelector('#phone')
const hidden = document.querySelector('input[name="phone"]')
const rawOutput = document.querySelector('[data-raw]')
const country = document.querySelector('#phone-country')
const options = {}

const update = () => {
  const { formatted, raw } = format(field.value, 'phone', { ...options, country: country.value })
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
}

field.addEventListener('input', update)
country.addEventListener('change', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A phone number for any country</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="phone-country">Country</label>
        <select id="phone-country">
          <option value="MX" selected>Mexico</option>
          <option value="US">United States</option>
          <option value="ES">Spain</option>
          <option value="GB">United Kingdom</option>
          <option value="AR">Argentina</option>
          <option value="DE">Germany</option>
        </select>
        <label for="phone">Mobile phone</label>
        <input
          id="phone"
          type="text"
          inputmode="tel"
          autocomplete="tel"
          placeholder="55 1234 5678"
          value="5512345678"
          aria-describedby="phone-raw"
        />
        <input type="hidden" name="phone" />
        <p id="phone-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script type="module">
      import { format } from '@samline/formatter'
      import { registerPhone } from '@samline/formatter/phone'

      registerPhone()

      const field = document.querySelector('#phone')
      const hidden = document.querySelector('input[name="phone"]')
      const rawOutput = document.querySelector('[data-raw]')
      const country = document.querySelector('#phone-country')
      const options = {}

      const update = () => {
        const { formatted, raw } = format(field.value, 'phone', {
          ...options,
          country: country.value,
        })
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
      }

      field.addEventListener('input', update)
      country.addEventListener('change', update)
      update()
    </script>
  </body>
</html>

Numbers and money

Amount with currency

The numeral type adds grouping, a prefix and a decimal limit. raw is a plain decimal string without the prefix. regex.numeral(options) takes the same options as format(), so the rule accepts exactly what the mask writes: $1,234. (decimal mark last) fails until a decimal is typed.

AmountLive · formatted and raw

An amount with currency and decimals

format(value, 'numeral', { prefix, numeralDecimalScale })

Thousands separators and a currency prefix appear while typing. The raw value is a plain decimal string ready to parse on the server.

Shown to people
$1,234,567.89
Sent to your backend
1234567.89
regex.numeral(options)
Matches the mask

numeralDecimalScale limits the decimals. The prefix is display-only: raw never includes it. regex.numeral(options) takes the same options, so the rule accepts exactly what the mask writes.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An amount with currency and decimals</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="amount">Invoice total</label>
        <input
          id="amount"
          type="text"
          inputmode="decimal"
          autocomplete="off"
          placeholder="$0.00"
          value="1234567.891"
          aria-describedby="amount-raw amount-status"
        />
        <input type="hidden" name="amount" />
        <p id="amount-raw">Sent to your backend: <output data-raw></output></p>
        <p id="amount-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter

const field = document.querySelector('#amount')
const hidden = document.querySelector('input[name="amount"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  prefix: '$',
  numeralDecimalScale: 2,
}
const rule = regex.numeral(options)

const update = () => {
  const { formatted, raw } = format(field.value, 'numeral', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Matches the mask' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An amount with currency and decimals</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="amount">Invoice total</label>
        <input
          id="amount"
          type="text"
          inputmode="decimal"
          autocomplete="off"
          placeholder="$0.00"
          value="1234567.891"
          aria-describedby="amount-raw amount-status"
        />
        <input type="hidden" name="amount" />
        <p id="amount-raw">Sent to your backend: <output data-raw></output></p>
        <p id="amount-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter

      const field = document.querySelector('#amount')
      const hidden = document.querySelector('input[name="amount"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        prefix: '$',
        numeralDecimalScale: 2,
      }
      const rule = regex.numeral(options)

      const update = () => {
        const { formatted, raw } = format(field.value, 'numeral', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Matches the mask' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An amount with currency and decimals</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="amount">Invoice total</label>
        <input
          id="amount"
          type="text"
          inputmode="decimal"
          autocomplete="off"
          placeholder="$0.00"
          value="1234567.891"
          aria-describedby="amount-raw amount-status"
        />
        <input type="hidden" name="amount" />
        <p id="amount-raw">Sent to your backend: <output data-raw></output></p>
        <p id="amount-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'

const field = document.querySelector('#amount')
const hidden = document.querySelector('input[name="amount"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  prefix: '$',
  numeralDecimalScale: 2,
}
const rule = regex.numeral(options)

const update = () => {
  const { formatted, raw } = format(field.value, 'numeral', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Matches the mask' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An amount with currency and decimals</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="amount">Invoice total</label>
        <input
          id="amount"
          type="text"
          inputmode="decimal"
          autocomplete="off"
          placeholder="$0.00"
          value="1234567.891"
          aria-describedby="amount-raw amount-status"
        />
        <input type="hidden" name="amount" />
        <p id="amount-raw">Sent to your backend: <output data-raw></output></p>
        <p id="amount-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'

      const field = document.querySelector('#amount')
      const hidden = document.querySelector('input[name="amount"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        prefix: '$',
        numeralDecimalScale: 2,
      }
      const rule = regex.numeral(options)

      const update = () => {
        const { formatted, raw } = format(field.value, 'numeral', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Matches the mask' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

European price

numeralDecimalMark: ',' and delimiter: '.' switch the separators, and tailPrefix: true moves the currency to the end. The raw value still uses a dot. The same options passed to regex.numeral(options) check the comma, the dots and the trailing € of the visible value.

Euro priceLive · formatted and raw

A European price with the symbol after it

format(value, 'numeral', { numeralDecimalMark, delimiter, numeralDecimalScale, prefix, tailPrefix })

A comma for decimals, dots for thousands and the currency after the number. The raw value still uses a dot, so the server parses it the same way.

Shown to people
1.234.567,89 €
Sent to your backend
1234567.89
regex.numeral(options)
Matches the mask

numeralDecimalMark and delimiter switch the separators; tailPrefix moves the prefix to the end. regex.numeral(options) checks the comma, the dots and the trailing symbol.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A European price with the symbol after it</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="price-eur">Price</label>
        <input
          id="price-eur"
          type="text"
          inputmode="decimal"
          autocomplete="off"
          placeholder="0,00 €"
          value="1234567,891"
          aria-describedby="price-eur-raw price-eur-status"
        />
        <input type="hidden" name="price" />
        <p id="price-eur-raw">Sent to your backend: <output data-raw></output></p>
        <p id="price-eur-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter

const field = document.querySelector('#price-eur')
const hidden = document.querySelector('input[name="price"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  numeralDecimalMark: ',',
  delimiter: '.',
  numeralDecimalScale: 2,
  prefix: ' €',
  tailPrefix: true,
}
const rule = regex.numeral(options)

const update = () => {
  const { formatted, raw } = format(field.value, 'numeral', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Matches the mask' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A European price with the symbol after it</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="price-eur">Price</label>
        <input
          id="price-eur"
          type="text"
          inputmode="decimal"
          autocomplete="off"
          placeholder="0,00 €"
          value="1234567,891"
          aria-describedby="price-eur-raw price-eur-status"
        />
        <input type="hidden" name="price" />
        <p id="price-eur-raw">Sent to your backend: <output data-raw></output></p>
        <p id="price-eur-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter

      const field = document.querySelector('#price-eur')
      const hidden = document.querySelector('input[name="price"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        numeralDecimalMark: ',',
        delimiter: '.',
        numeralDecimalScale: 2,
        prefix: ' €',
        tailPrefix: true,
      }
      const rule = regex.numeral(options)

      const update = () => {
        const { formatted, raw } = format(field.value, 'numeral', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Matches the mask' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A European price with the symbol after it</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="price-eur">Price</label>
        <input
          id="price-eur"
          type="text"
          inputmode="decimal"
          autocomplete="off"
          placeholder="0,00 €"
          value="1234567,891"
          aria-describedby="price-eur-raw price-eur-status"
        />
        <input type="hidden" name="price" />
        <p id="price-eur-raw">Sent to your backend: <output data-raw></output></p>
        <p id="price-eur-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'

const field = document.querySelector('#price-eur')
const hidden = document.querySelector('input[name="price"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  numeralDecimalMark: ',',
  delimiter: '.',
  numeralDecimalScale: 2,
  prefix: ' €',
  tailPrefix: true,
}
const rule = regex.numeral(options)

const update = () => {
  const { formatted, raw } = format(field.value, 'numeral', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Matches the mask' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A European price with the symbol after it</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="price-eur">Price</label>
        <input
          id="price-eur"
          type="text"
          inputmode="decimal"
          autocomplete="off"
          placeholder="0,00 €"
          value="1234567,891"
          aria-describedby="price-eur-raw price-eur-status"
        />
        <input type="hidden" name="price" />
        <p id="price-eur-raw">Sent to your backend: <output data-raw></output></p>
        <p id="price-eur-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'

      const field = document.querySelector('#price-eur')
      const hidden = document.querySelector('input[name="price"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        numeralDecimalMark: ',',
        delimiter: '.',
        numeralDecimalScale: 2,
        prefix: ' €',
        tailPrefix: true,
      }
      const rule = regex.numeral(options)

      const update = () => {
        const { formatted, raw } = format(field.value, 'numeral', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Matches the mask' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Whole positive quantity

numeralDecimalScale: 0 and numeralPositiveOnly: true produce integer counts with no sign. Leading zeros are removed.

QuantityLive · formatted and raw

A whole, positive quantity

format(value, 'numeral', { numeralDecimalScale, numeralPositiveOnly })

Stock counts and seat numbers never need decimals or a minus sign. Leading zeros disappear and large counts stay readable.

Shown to people
1,500
Sent to your backend
1500

numeralDecimalScale: 0 drops decimals and numeralPositiveOnly drops the sign. The raw value is an integer string.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A whole, positive quantity</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="quantity">Units in stock</label>
        <input
          id="quantity"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="0"
          value="1500"
          aria-describedby="quantity-raw"
        />
        <input type="hidden" name="quantity" />
        <p id="quantity-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format } = window.Formatter

const field = document.querySelector('#quantity')
const hidden = document.querySelector('input[name="quantity"]')
const rawOutput = document.querySelector('[data-raw]')
const options = {
  numeralDecimalScale: 0,
  numeralPositiveOnly: true,
}

const update = () => {
  const { formatted, raw } = format(field.value, 'numeral', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A whole, positive quantity</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="quantity">Units in stock</label>
        <input
          id="quantity"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="0"
          value="1500"
          aria-describedby="quantity-raw"
        />
        <input type="hidden" name="quantity" />
        <p id="quantity-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format } = window.Formatter

      const field = document.querySelector('#quantity')
      const hidden = document.querySelector('input[name="quantity"]')
      const rawOutput = document.querySelector('[data-raw]')
      const options = {
        numeralDecimalScale: 0,
        numeralPositiveOnly: true,
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'numeral', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A whole, positive quantity</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="quantity">Units in stock</label>
        <input
          id="quantity"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="0"
          value="1500"
          aria-describedby="quantity-raw"
        />
        <input type="hidden" name="quantity" />
        <p id="quantity-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format } from '@samline/formatter'

const field = document.querySelector('#quantity')
const hidden = document.querySelector('input[name="quantity"]')
const rawOutput = document.querySelector('[data-raw]')
const options = {
  numeralDecimalScale: 0,
  numeralPositiveOnly: true,
}

const update = () => {
  const { formatted, raw } = format(field.value, 'numeral', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A whole, positive quantity</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="quantity">Units in stock</label>
        <input
          id="quantity"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="0"
          value="1500"
          aria-describedby="quantity-raw"
        />
        <input type="hidden" name="quantity" />
        <p id="quantity-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script type="module">
      import { format } from '@samline/formatter'

      const field = document.querySelector('#quantity')
      const hidden = document.querySelector('input[name="quantity"]')
      const rawOutput = document.querySelector('[data-raw]')
      const options = {
        numeralDecimalScale: 0,
        numeralPositiveOnly: true,
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'numeral', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Dates and times

Date for display, ISO for storage

dateRawPattern and dateRawPatternDelimiter decide the raw shape independently of the display pattern. The default interpretInputAs: 'auto' converts a stored 1990-12-31 for display.

DateLive · formatted and raw

A date shown locally, stored as ISO

format(value, 'date', { datePattern, delimiter, dateRawPattern, dateRawPatternDelimiter })

People read day/month/year; the API stores year-month-day. A server value such as 1990-12-31 is recognized and converted for display.

Shown to people
31/12/1990
Sent to your backend
1990-12-31
regex.date
Please enter a valid date (DD/MM/YYYY).

interpretInputAs: "auto" treats a complete value without the display delimiter as raw. Partial or delimited values are treated as typing.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A date shown locally, stored as ISO</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="birth-date">Date of birth</label>
        <input
          id="birth-date"
          type="text"
          inputmode="numeric"
          autocomplete="bday"
          placeholder="dd/mm/yyyy"
          value="1990-12-31"
          aria-describedby="birth-date-raw birth-date-status"
        />
        <input type="hidden" name="birth_date" />
        <p id="birth-date-raw">Sent to your backend: <output data-raw></output></p>
        <p id="birth-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter

const field = document.querySelector('#birth-date')
const hidden = document.querySelector('input[name="birth_date"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  datePattern: ['d', 'm', 'Y'],
  delimiter: '/',
  dateRawPattern: ['Y', 'm', 'd'],
  dateRawPatternDelimiter: '-',
}
const rule = regex.date

const update = () => {
  const { formatted, raw } = format(field.value, 'date', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(raw)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Complete ISO date' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A date shown locally, stored as ISO</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="birth-date">Date of birth</label>
        <input
          id="birth-date"
          type="text"
          inputmode="numeric"
          autocomplete="bday"
          placeholder="dd/mm/yyyy"
          value="1990-12-31"
          aria-describedby="birth-date-raw birth-date-status"
        />
        <input type="hidden" name="birth_date" />
        <p id="birth-date-raw">Sent to your backend: <output data-raw></output></p>
        <p id="birth-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter

      const field = document.querySelector('#birth-date')
      const hidden = document.querySelector('input[name="birth_date"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        datePattern: ['d', 'm', 'Y'],
        delimiter: '/',
        dateRawPattern: ['Y', 'm', 'd'],
        dateRawPatternDelimiter: '-',
      }
      const rule = regex.date

      const update = () => {
        const { formatted, raw } = format(field.value, 'date', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(raw)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Complete ISO date' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A date shown locally, stored as ISO</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="birth-date">Date of birth</label>
        <input
          id="birth-date"
          type="text"
          inputmode="numeric"
          autocomplete="bday"
          placeholder="dd/mm/yyyy"
          value="1990-12-31"
          aria-describedby="birth-date-raw birth-date-status"
        />
        <input type="hidden" name="birth_date" />
        <p id="birth-date-raw">Sent to your backend: <output data-raw></output></p>
        <p id="birth-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'

const field = document.querySelector('#birth-date')
const hidden = document.querySelector('input[name="birth_date"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  datePattern: ['d', 'm', 'Y'],
  delimiter: '/',
  dateRawPattern: ['Y', 'm', 'd'],
  dateRawPatternDelimiter: '-',
}
const rule = regex.date

const update = () => {
  const { formatted, raw } = format(field.value, 'date', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(raw)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Complete ISO date' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A date shown locally, stored as ISO</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="birth-date">Date of birth</label>
        <input
          id="birth-date"
          type="text"
          inputmode="numeric"
          autocomplete="bday"
          placeholder="dd/mm/yyyy"
          value="1990-12-31"
          aria-describedby="birth-date-raw birth-date-status"
        />
        <input type="hidden" name="birth_date" />
        <p id="birth-date-raw">Sent to your backend: <output data-raw></output></p>
        <p id="birth-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'

      const field = document.querySelector('#birth-date')
      const hidden = document.querySelector('input[name="birth_date"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        datePattern: ['d', 'm', 'Y'],
        delimiter: '/',
        dateRawPattern: ['Y', 'm', 'd'],
        dateRawPatternDelimiter: '-',
      }
      const rule = regex.date

      const update = () => {
        const { formatted, raw } = format(field.value, 'date', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(raw)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Complete ISO date' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Card expiry

datePattern: ['m', 'y'] builds an MM/YY mask and corrects impossible months. Pair it with regex.expirationDate.

Card expiryLive · formatted and raw

A card expiry date as MM/YY

format(value, 'date', { datePattern, delimiter, dateRawPatternDelimiter })

Month and two-digit year with the slash inserted automatically. An impossible month such as 13 is corrected while typing.

Shown to people
12/28
Sent to your backend
12/28
regex.expirationDate
Valid expiry format

datePattern: ["m", "y"] builds the mask. Setting dateRawPatternDelimiter keeps the slash in raw for payment forms that expect MM/YY.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A card expiry date as MM/YY</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="card-expiry">Expiry date</label>
        <input
          id="card-expiry"
          type="text"
          inputmode="numeric"
          autocomplete="cc-exp"
          placeholder="MM/YY"
          value="1228"
          aria-describedby="card-expiry-raw card-expiry-status"
        />
        <input type="hidden" name="card_expiry" />
        <p id="card-expiry-raw">Sent to your backend: <output data-raw></output></p>
        <p id="card-expiry-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter

const field = document.querySelector('#card-expiry')
const hidden = document.querySelector('input[name="card_expiry"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  datePattern: ['m', 'y'],
  delimiter: '/',
  dateRawPatternDelimiter: '/',
}
const rule = regex.expirationDate

const update = () => {
  const { formatted, raw } = format(field.value, 'date', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid expiry format' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A card expiry date as MM/YY</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="card-expiry">Expiry date</label>
        <input
          id="card-expiry"
          type="text"
          inputmode="numeric"
          autocomplete="cc-exp"
          placeholder="MM/YY"
          value="1228"
          aria-describedby="card-expiry-raw card-expiry-status"
        />
        <input type="hidden" name="card_expiry" />
        <p id="card-expiry-raw">Sent to your backend: <output data-raw></output></p>
        <p id="card-expiry-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter

      const field = document.querySelector('#card-expiry')
      const hidden = document.querySelector('input[name="card_expiry"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        datePattern: ['m', 'y'],
        delimiter: '/',
        dateRawPatternDelimiter: '/',
      }
      const rule = regex.expirationDate

      const update = () => {
        const { formatted, raw } = format(field.value, 'date', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid expiry format' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A card expiry date as MM/YY</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="card-expiry">Expiry date</label>
        <input
          id="card-expiry"
          type="text"
          inputmode="numeric"
          autocomplete="cc-exp"
          placeholder="MM/YY"
          value="1228"
          aria-describedby="card-expiry-raw card-expiry-status"
        />
        <input type="hidden" name="card_expiry" />
        <p id="card-expiry-raw">Sent to your backend: <output data-raw></output></p>
        <p id="card-expiry-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'

const field = document.querySelector('#card-expiry')
const hidden = document.querySelector('input[name="card_expiry"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  datePattern: ['m', 'y'],
  delimiter: '/',
  dateRawPatternDelimiter: '/',
}
const rule = regex.expirationDate

const update = () => {
  const { formatted, raw } = format(field.value, 'date', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid expiry format' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A card expiry date as MM/YY</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="card-expiry">Expiry date</label>
        <input
          id="card-expiry"
          type="text"
          inputmode="numeric"
          autocomplete="cc-exp"
          placeholder="MM/YY"
          value="1228"
          aria-describedby="card-expiry-raw card-expiry-status"
        />
        <input type="hidden" name="card_expiry" />
        <p id="card-expiry-raw">Sent to your backend: <output data-raw></output></p>
        <p id="card-expiry-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'

      const field = document.querySelector('#card-expiry')
      const hidden = document.querySelector('input[name="card_expiry"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        datePattern: ['m', 'y'],
        delimiter: '/',
        dateRawPatternDelimiter: '/',
      }
      const rule = regex.expirationDate

      const update = () => {
        const { formatted, raw } = format(field.value, 'date', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid expiry format' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Appointment time

timePattern: ['h', 'm'] shows hours and minutes. The raw value is validated with regex.time24.

TimeLive · formatted and raw

A 24-hour appointment time

format(value, 'time', { timePattern })

Digits become hours and minutes, and out-of-range values are corrected while typing. The raw value is ready for a time column.

Shown to people
09:30
Sent to your backend
09:30
regex.time24
Valid 24-hour time

timePattern sets the visible segments. The raw value uses timeRawPattern, which follows timePattern unless you set it.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A 24-hour appointment time</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="appointment">Appointment time</label>
        <input
          id="appointment"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="hh:mm"
          value="0930"
          aria-describedby="appointment-raw appointment-status"
        />
        <input type="hidden" name="appointment" />
        <p id="appointment-raw">Sent to your backend: <output data-raw></output></p>
        <p id="appointment-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter

const field = document.querySelector('#appointment')
const hidden = document.querySelector('input[name="appointment"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  timePattern: ['h', 'm'],
}
const rule = regex.time24

const update = () => {
  const { formatted, raw } = format(field.value, 'time', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(raw)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid 24-hour time' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A 24-hour appointment time</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="appointment">Appointment time</label>
        <input
          id="appointment"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="hh:mm"
          value="0930"
          aria-describedby="appointment-raw appointment-status"
        />
        <input type="hidden" name="appointment" />
        <p id="appointment-raw">Sent to your backend: <output data-raw></output></p>
        <p id="appointment-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter

      const field = document.querySelector('#appointment')
      const hidden = document.querySelector('input[name="appointment"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        timePattern: ['h', 'm'],
      }
      const rule = regex.time24

      const update = () => {
        const { formatted, raw } = format(field.value, 'time', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(raw)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid 24-hour time' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A 24-hour appointment time</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="appointment">Appointment time</label>
        <input
          id="appointment"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="hh:mm"
          value="0930"
          aria-describedby="appointment-raw appointment-status"
        />
        <input type="hidden" name="appointment" />
        <p id="appointment-raw">Sent to your backend: <output data-raw></output></p>
        <p id="appointment-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'

const field = document.querySelector('#appointment')
const hidden = document.querySelector('input[name="appointment"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  timePattern: ['h', 'm'],
}
const rule = regex.time24

const update = () => {
  const { formatted, raw } = format(field.value, 'time', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(raw)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid 24-hour time' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A 24-hour appointment time</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="appointment">Appointment time</label>
        <input
          id="appointment"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="hh:mm"
          value="0930"
          aria-describedby="appointment-raw appointment-status"
        />
        <input type="hidden" name="appointment" />
        <p id="appointment-raw">Sent to your backend: <output data-raw></output></p>
        <p id="appointment-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'

      const field = document.querySelector('#appointment')
      const hidden = document.querySelector('input[name="appointment"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        timePattern: ['h', 'm'],
      }
      const rule = regex.time24

      const update = () => {
        const { formatted, raw } = format(field.value, 'time', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(raw)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid 24-hour time' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Date tools in any language

The mask writes DD/MM/YYYY; parseDate from @samline/formatter/date reads it back with the same tokens and formatDatePattern shows the weekday (dddd), the long date (LL) and the ISO week (WW) in the selected locale. parseDate is strict, so it returns undefined until the date is complete and real, and regex.date(options) reports the same state for the visible value. The copied ESM file imports @samline/formatter/date; the CDN file loads formatter.date.global.js after the core script and reads window.Formatter.date. See Date tools for every function.

Date toolsLive · formatted and raw

A typed date read back in any language

format(value, 'date', { datePattern, delimiter })

The mask writes day/month/year; parseDate reads it with the same tokens and formatDatePattern shows the weekday, the long date and the ISO week in the selected locale.

Shown to people
24/12/2026
Sent to your backend
24/12/2026
Weekday
jueves
Long date
24 de diciembre de 2026
ISO week
52
regex.date(options)
Complete date

The date tools live in @samline/formatter/date (CDN: formatter.date.global.js after the core script). parseDate is strict, so it returns undefined until the date is complete and real.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A typed date read back in any language</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="date-locale">Language</label>
        <select id="date-locale">
          <option value="es-MX" selected>Spanish (Mexico)</option>
          <option value="en-US">English (US)</option>
          <option value="pt-BR">Portuguese (Brazil)</option>
          <option value="fr-FR">French</option>
          <option value="de-DE">German</option>
        </select>
        <label for="event-date">Event date</label>
        <input
          id="event-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="dd/mm/yyyy"
          value="24122026"
          aria-describedby="event-date-raw event-date-status"
        />
        <input type="hidden" name="event_date" />
        <p id="event-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>Weekday: <output data-date></output></p>
        <p>Long date: <output data-date></output></p>
        <p>ISO week: <output data-date></output></p>
        <p id="event-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.date.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter
const { formatDatePattern, parseDate } = window.Formatter.date

const field = document.querySelector('#event-date')
const hidden = document.querySelector('input[name="event_date"]')
const rawOutput = document.querySelector('[data-raw]')
const locale = document.querySelector('#date-locale')
const dateOutputs = document.querySelectorAll('[data-date]')
const status = document.querySelector('[data-status]')
const options = {
  datePattern: ['d', 'm', 'Y'],
  delimiter: '/',
}
const rule = regex.date(options)
const datePatterns = ['dddd', 'LL', 'WW']

const update = () => {
  const { formatted, raw } = format(field.value, 'date', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const date = parseDate(formatted, { format: 'DD/MM/YYYY' })
  dateOutputs.forEach((output, index) => {
    output.value = date
      ? formatDatePattern(date, datePatterns[index], { locale: locale.value })
      : 'Keep typing'
  })
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Complete date' : rule.errorMessage
}

field.addEventListener('input', update)
locale.addEventListener('change', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A typed date read back in any language</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="date-locale">Language</label>
        <select id="date-locale">
          <option value="es-MX" selected>Spanish (Mexico)</option>
          <option value="en-US">English (US)</option>
          <option value="pt-BR">Portuguese (Brazil)</option>
          <option value="fr-FR">French</option>
          <option value="de-DE">German</option>
        </select>
        <label for="event-date">Event date</label>
        <input
          id="event-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="dd/mm/yyyy"
          value="24122026"
          aria-describedby="event-date-raw event-date-status"
        />
        <input type="hidden" name="event_date" />
        <p id="event-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>Weekday: <output data-date></output></p>
        <p>Long date: <output data-date></output></p>
        <p>ISO week: <output data-date></output></p>
        <p id="event-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.date.global.js"></script>
    <script>
      const { format, regex } = window.Formatter
      const { formatDatePattern, parseDate } = window.Formatter.date

      const field = document.querySelector('#event-date')
      const hidden = document.querySelector('input[name="event_date"]')
      const rawOutput = document.querySelector('[data-raw]')
      const locale = document.querySelector('#date-locale')
      const dateOutputs = document.querySelectorAll('[data-date]')
      const status = document.querySelector('[data-status]')
      const options = {
        datePattern: ['d', 'm', 'Y'],
        delimiter: '/',
      }
      const rule = regex.date(options)
      const datePatterns = ['dddd', 'LL', 'WW']

      const update = () => {
        const { formatted, raw } = format(field.value, 'date', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const date = parseDate(formatted, { format: 'DD/MM/YYYY' })
        dateOutputs.forEach((output, index) => {
          output.value = date
            ? formatDatePattern(date, datePatterns[index], { locale: locale.value })
            : 'Keep typing'
        })
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Complete date' : rule.errorMessage
      }

      field.addEventListener('input', update)
      locale.addEventListener('change', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A typed date read back in any language</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="date-locale">Language</label>
        <select id="date-locale">
          <option value="es-MX" selected>Spanish (Mexico)</option>
          <option value="en-US">English (US)</option>
          <option value="pt-BR">Portuguese (Brazil)</option>
          <option value="fr-FR">French</option>
          <option value="de-DE">German</option>
        </select>
        <label for="event-date">Event date</label>
        <input
          id="event-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="dd/mm/yyyy"
          value="24122026"
          aria-describedby="event-date-raw event-date-status"
        />
        <input type="hidden" name="event_date" />
        <p id="event-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>Weekday: <output data-date></output></p>
        <p>Long date: <output data-date></output></p>
        <p>ISO week: <output data-date></output></p>
        <p id="event-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'
import { formatDatePattern, parseDate } from '@samline/formatter/date'

const field = document.querySelector('#event-date')
const hidden = document.querySelector('input[name="event_date"]')
const rawOutput = document.querySelector('[data-raw]')
const locale = document.querySelector('#date-locale')
const dateOutputs = document.querySelectorAll('[data-date]')
const status = document.querySelector('[data-status]')
const options = {
  datePattern: ['d', 'm', 'Y'],
  delimiter: '/',
}
const rule = regex.date(options)
const datePatterns = ['dddd', 'LL', 'WW']

const update = () => {
  const { formatted, raw } = format(field.value, 'date', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const date = parseDate(formatted, { format: 'DD/MM/YYYY' })
  dateOutputs.forEach((output, index) => {
    output.value = date
      ? formatDatePattern(date, datePatterns[index], { locale: locale.value })
      : 'Keep typing'
  })
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Complete date' : rule.errorMessage
}

field.addEventListener('input', update)
locale.addEventListener('change', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A typed date read back in any language</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="date-locale">Language</label>
        <select id="date-locale">
          <option value="es-MX" selected>Spanish (Mexico)</option>
          <option value="en-US">English (US)</option>
          <option value="pt-BR">Portuguese (Brazil)</option>
          <option value="fr-FR">French</option>
          <option value="de-DE">German</option>
        </select>
        <label for="event-date">Event date</label>
        <input
          id="event-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="dd/mm/yyyy"
          value="24122026"
          aria-describedby="event-date-raw event-date-status"
        />
        <input type="hidden" name="event_date" />
        <p id="event-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>Weekday: <output data-date></output></p>
        <p>Long date: <output data-date></output></p>
        <p>ISO week: <output data-date></output></p>
        <p id="event-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'
      import { formatDatePattern, parseDate } from '@samline/formatter/date'

      const field = document.querySelector('#event-date')
      const hidden = document.querySelector('input[name="event_date"]')
      const rawOutput = document.querySelector('[data-raw]')
      const locale = document.querySelector('#date-locale')
      const dateOutputs = document.querySelectorAll('[data-date]')
      const status = document.querySelector('[data-status]')
      const options = {
        datePattern: ['d', 'm', 'Y'],
        delimiter: '/',
      }
      const rule = regex.date(options)
      const datePatterns = ['dddd', 'LL', 'WW']

      const update = () => {
        const { formatted, raw } = format(field.value, 'date', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const date = parseDate(formatted, { format: 'DD/MM/YYYY' })
        dateOutputs.forEach((output, index) => {
          output.value = date
            ? formatDatePattern(date, datePatterns[index], { locale: locale.value })
            : 'Keep typing'
        })
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Complete date' : rule.errorMessage
      }

      field.addEventListener('input', update)
      locale.addEventListener('change', update)
      update()
    </script>
  </body>
</html>

Check a date strictly

This field has no mask, so nothing corrects the value: it shows what parseDate does with a date that does not exist. isValidDate(value, { format: 'YYYY-MM-DD' }) is false for 2026-02-30, and parseDate returns undefined instead of rolling it over to March 2 the way new Date(2026, 1, 30) does. There is no error object, so the copied code explains the rejection itself with daysInMonth: February 2026 has 28 days., Month 13 does not exist. or a reminder of the YYYY-MM-DD form.

Strict dateLive · formatted and raw

Check a date strictly

format(value, 'text')

A date typed or pasted as YYYY-MM-DD, with no mask to correct it. isValidDate says whether it exists; parseDate returns undefined for 2026-02-30 instead of rolling it over to March 2.

Shown to people
2026-02-30
Sent to your backend
2026-02-30
Read as
—
isValidDate(value, { format })
February 2026 has 28 days.

new Date(2026, 1, 30) silently becomes March 2. parseDate rejects impossible days, months and times; the copied code explains why with daysInMonth.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Check a date strictly</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="check-date">Date (YYYY-MM-DD)</label>
        <input
          id="check-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="YYYY-MM-DD"
          value="2026-02-30"
          aria-describedby="check-date-raw check-date-status"
        />
        <input type="hidden" name="check_date" />
        <p id="check-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>Read as: <output data-date></output></p>
        <p id="check-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.date.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format } = window.Formatter
const { daysInMonth, formatDatePattern, isValidDate, parseDate } = window.Formatter.date

const field = document.querySelector('#check-date')
const hidden = document.querySelector('input[name="check_date"]')
const rawOutput = document.querySelector('[data-raw]')
const dateOutputs = document.querySelectorAll('[data-date]')
const status = document.querySelector('[data-status]')
const options = {}

// parseDate only says undefined; this explains the most common reasons.
const rejectReason = text => {
  const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text)
  if (!match) return 'Use the YYYY-MM-DD form, for example 2026-02-28.'
  const [year, month, day] = match.slice(1).map(Number)
  if (month < 1 || month > 12) return `Month ${month} does not exist.`
  const first = new Date(year, month - 1, 1)
  const days = daysInMonth(first)
  if (day < 1 || day > days)
    return `${formatDatePattern(first, 'MMMM YYYY', { locale: 'en-US' })} has ${days} days.`
  return 'Not a valid date.'
}

const update = () => {
  const { formatted, raw } = format(field.value, 'text', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const date = parseDate(formatted, { format: 'YYYY-MM-DD' })
  const valid = isValidDate(formatted, { format: 'YYYY-MM-DD' })
  dateOutputs[0].value = date
    ? formatDatePattern(date, 'dddd, MMMM D, YYYY', { locale: 'en-US' })
    : '—'
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid date' : rejectReason(formatted)
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Check a date strictly</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="check-date">Date (YYYY-MM-DD)</label>
        <input
          id="check-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="YYYY-MM-DD"
          value="2026-02-30"
          aria-describedby="check-date-raw check-date-status"
        />
        <input type="hidden" name="check_date" />
        <p id="check-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>Read as: <output data-date></output></p>
        <p id="check-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.date.global.js"></script>
    <script>
      const { format } = window.Formatter
      const { daysInMonth, formatDatePattern, isValidDate, parseDate } = window.Formatter.date

      const field = document.querySelector('#check-date')
      const hidden = document.querySelector('input[name="check_date"]')
      const rawOutput = document.querySelector('[data-raw]')
      const dateOutputs = document.querySelectorAll('[data-date]')
      const status = document.querySelector('[data-status]')
      const options = {}

      // parseDate only says undefined; this explains the most common reasons.
      const rejectReason = text => {
        const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text)
        if (!match) return 'Use the YYYY-MM-DD form, for example 2026-02-28.'
        const [year, month, day] = match.slice(1).map(Number)
        if (month < 1 || month > 12) return `Month ${month} does not exist.`
        const first = new Date(year, month - 1, 1)
        const days = daysInMonth(first)
        if (day < 1 || day > days)
          return `${formatDatePattern(first, 'MMMM YYYY', { locale: 'en-US' })} has ${days} days.`
        return 'Not a valid date.'
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'text', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const date = parseDate(formatted, { format: 'YYYY-MM-DD' })
        const valid = isValidDate(formatted, { format: 'YYYY-MM-DD' })
        dateOutputs[0].value = date
          ? formatDatePattern(date, 'dddd, MMMM D, YYYY', { locale: 'en-US' })
          : '—'
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid date' : rejectReason(formatted)
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Check a date strictly</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="check-date">Date (YYYY-MM-DD)</label>
        <input
          id="check-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="YYYY-MM-DD"
          value="2026-02-30"
          aria-describedby="check-date-raw check-date-status"
        />
        <input type="hidden" name="check_date" />
        <p id="check-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>Read as: <output data-date></output></p>
        <p id="check-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format } from '@samline/formatter'
import { daysInMonth, formatDatePattern, isValidDate, parseDate } from '@samline/formatter/date'

const field = document.querySelector('#check-date')
const hidden = document.querySelector('input[name="check_date"]')
const rawOutput = document.querySelector('[data-raw]')
const dateOutputs = document.querySelectorAll('[data-date]')
const status = document.querySelector('[data-status]')
const options = {}

// parseDate only says undefined; this explains the most common reasons.
const rejectReason = text => {
  const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text)
  if (!match) return 'Use the YYYY-MM-DD form, for example 2026-02-28.'
  const [year, month, day] = match.slice(1).map(Number)
  if (month < 1 || month > 12) return `Month ${month} does not exist.`
  const first = new Date(year, month - 1, 1)
  const days = daysInMonth(first)
  if (day < 1 || day > days)
    return `${formatDatePattern(first, 'MMMM YYYY', { locale: 'en-US' })} has ${days} days.`
  return 'Not a valid date.'
}

const update = () => {
  const { formatted, raw } = format(field.value, 'text', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const date = parseDate(formatted, { format: 'YYYY-MM-DD' })
  const valid = isValidDate(formatted, { format: 'YYYY-MM-DD' })
  dateOutputs[0].value = date
    ? formatDatePattern(date, 'dddd, MMMM D, YYYY', { locale: 'en-US' })
    : '—'
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid date' : rejectReason(formatted)
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Check a date strictly</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="check-date">Date (YYYY-MM-DD)</label>
        <input
          id="check-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="YYYY-MM-DD"
          value="2026-02-30"
          aria-describedby="check-date-raw check-date-status"
        />
        <input type="hidden" name="check_date" />
        <p id="check-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>Read as: <output data-date></output></p>
        <p id="check-date-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format } from '@samline/formatter'
      import {
        daysInMonth,
        formatDatePattern,
        isValidDate,
        parseDate,
      } from '@samline/formatter/date'

      const field = document.querySelector('#check-date')
      const hidden = document.querySelector('input[name="check_date"]')
      const rawOutput = document.querySelector('[data-raw]')
      const dateOutputs = document.querySelectorAll('[data-date]')
      const status = document.querySelector('[data-status]')
      const options = {}

      // parseDate only says undefined; this explains the most common reasons.
      const rejectReason = text => {
        const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text)
        if (!match) return 'Use the YYYY-MM-DD form, for example 2026-02-28.'
        const [year, month, day] = match.slice(1).map(Number)
        if (month < 1 || month > 12) return `Month ${month} does not exist.`
        const first = new Date(year, month - 1, 1)
        const days = daysInMonth(first)
        if (day < 1 || day > days)
          return `${formatDatePattern(first, 'MMMM YYYY', { locale: 'en-US' })} has ${days} days.`
        return 'Not a valid date.'
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'text', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const date = parseDate(formatted, { format: 'YYYY-MM-DD' })
        const valid = isValidDate(formatted, { format: 'YYYY-MM-DD' })
        dateOutputs[0].value = date
          ? formatDatePattern(date, 'dddd, MMMM D, YYYY', { locale: 'en-US' })
          : '—'
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid date' : rejectReason(formatted)
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Move a date by months

addDate(date, months, 'month') adds calendar months and clamps the day, so 2026-01-31 plus one month is 2026-02-28, never March 3. endOfDate(result, 'month') gives the last day of that month: 2027-02-28 plus twelve months ends on 2028-02-29. The mask writes YYYY-MM-DD and parseDate reads it with the same tokens.

Move a dateLive · formatted and raw

Move a date by months

format(value, 'date', { datePattern, delimiter })

addDate adds calendar months and clamps the day to the end of shorter months, so January 31 plus one month is February 28. endOfDate then gives the last day of that month.

Shown to people
2026-01-31
Sent to your backend
2026-01-31
After adding
2026-02-28
End of that month
2026-02-28

The mask writes YYYY-MM-DD and parseDate reads it with the same tokens. formatDatePattern(endOfDate(addDate(date, months, "month"), "month"), "YYYY-MM-DD") is the whole calculation.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Move a date by months</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="months">Months to add</label>
        <select id="months">
          <option value="1" selected>+1 month</option>
          <option value="3">+3 months</option>
          <option value="6">+6 months</option>
          <option value="12">+12 months</option>
          <option value="-1">−1 month</option>
          <option value="-12">−12 months</option>
        </select>
        <label for="start-date">Start date</label>
        <input
          id="start-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="YYYY-MM-DD"
          value="2026-01-31"
          aria-describedby="start-date-raw"
        />
        <input type="hidden" name="start_date" />
        <p id="start-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>After adding: <output data-date></output></p>
        <p>End of that month: <output data-date></output></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.date.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format } = window.Formatter
const { addDate, endOfDate, formatDatePattern, parseDate } = window.Formatter.date

const field = document.querySelector('#start-date')
const hidden = document.querySelector('input[name="start_date"]')
const rawOutput = document.querySelector('[data-raw]')
const months = document.querySelector('#months')
const dateOutputs = document.querySelectorAll('[data-date]')
const options = {
  datePattern: ['Y', 'm', 'd'],
  delimiter: '-',
}

const update = () => {
  const { formatted, raw } = format(field.value, 'date', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const date = parseDate(formatted, { format: 'YYYY-MM-DD' })
  const moved = date && addDate(date, Number(months.value), 'month')
  const [added, end] = dateOutputs
  added.value = moved ? formatDatePattern(moved, 'YYYY-MM-DD') : 'Keep typing'
  end.value = moved ? formatDatePattern(endOfDate(moved, 'month'), 'YYYY-MM-DD') : 'Keep typing'
}

field.addEventListener('input', update)
months.addEventListener('change', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Move a date by months</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="months">Months to add</label>
        <select id="months">
          <option value="1" selected>+1 month</option>
          <option value="3">+3 months</option>
          <option value="6">+6 months</option>
          <option value="12">+12 months</option>
          <option value="-1">−1 month</option>
          <option value="-12">−12 months</option>
        </select>
        <label for="start-date">Start date</label>
        <input
          id="start-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="YYYY-MM-DD"
          value="2026-01-31"
          aria-describedby="start-date-raw"
        />
        <input type="hidden" name="start_date" />
        <p id="start-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>After adding: <output data-date></output></p>
        <p>End of that month: <output data-date></output></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.date.global.js"></script>
    <script>
      const { format } = window.Formatter
      const { addDate, endOfDate, formatDatePattern, parseDate } = window.Formatter.date

      const field = document.querySelector('#start-date')
      const hidden = document.querySelector('input[name="start_date"]')
      const rawOutput = document.querySelector('[data-raw]')
      const months = document.querySelector('#months')
      const dateOutputs = document.querySelectorAll('[data-date]')
      const options = {
        datePattern: ['Y', 'm', 'd'],
        delimiter: '-',
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'date', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const date = parseDate(formatted, { format: 'YYYY-MM-DD' })
        const moved = date && addDate(date, Number(months.value), 'month')
        const [added, end] = dateOutputs
        added.value = moved ? formatDatePattern(moved, 'YYYY-MM-DD') : 'Keep typing'
        end.value = moved
          ? formatDatePattern(endOfDate(moved, 'month'), 'YYYY-MM-DD')
          : 'Keep typing'
      }

      field.addEventListener('input', update)
      months.addEventListener('change', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Move a date by months</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="months">Months to add</label>
        <select id="months">
          <option value="1" selected>+1 month</option>
          <option value="3">+3 months</option>
          <option value="6">+6 months</option>
          <option value="12">+12 months</option>
          <option value="-1">−1 month</option>
          <option value="-12">−12 months</option>
        </select>
        <label for="start-date">Start date</label>
        <input
          id="start-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="YYYY-MM-DD"
          value="2026-01-31"
          aria-describedby="start-date-raw"
        />
        <input type="hidden" name="start_date" />
        <p id="start-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>After adding: <output data-date></output></p>
        <p>End of that month: <output data-date></output></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format } from '@samline/formatter'
import { addDate, endOfDate, formatDatePattern, parseDate } from '@samline/formatter/date'

const field = document.querySelector('#start-date')
const hidden = document.querySelector('input[name="start_date"]')
const rawOutput = document.querySelector('[data-raw]')
const months = document.querySelector('#months')
const dateOutputs = document.querySelectorAll('[data-date]')
const options = {
  datePattern: ['Y', 'm', 'd'],
  delimiter: '-',
}

const update = () => {
  const { formatted, raw } = format(field.value, 'date', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const date = parseDate(formatted, { format: 'YYYY-MM-DD' })
  const moved = date && addDate(date, Number(months.value), 'month')
  const [added, end] = dateOutputs
  added.value = moved ? formatDatePattern(moved, 'YYYY-MM-DD') : 'Keep typing'
  end.value = moved ? formatDatePattern(endOfDate(moved, 'month'), 'YYYY-MM-DD') : 'Keep typing'
}

field.addEventListener('input', update)
months.addEventListener('change', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Move a date by months</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="months">Months to add</label>
        <select id="months">
          <option value="1" selected>+1 month</option>
          <option value="3">+3 months</option>
          <option value="6">+6 months</option>
          <option value="12">+12 months</option>
          <option value="-1">−1 month</option>
          <option value="-12">−12 months</option>
        </select>
        <label for="start-date">Start date</label>
        <input
          id="start-date"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="YYYY-MM-DD"
          value="2026-01-31"
          aria-describedby="start-date-raw"
        />
        <input type="hidden" name="start_date" />
        <p id="start-date-raw">Sent to your backend: <output data-raw></output></p>
        <p>After adding: <output data-date></output></p>
        <p>End of that month: <output data-date></output></p>
      </div>
    </main>

    <script type="module">
      import { format } from '@samline/formatter'
      import { addDate, endOfDate, formatDatePattern, parseDate } from '@samline/formatter/date'

      const field = document.querySelector('#start-date')
      const hidden = document.querySelector('input[name="start_date"]')
      const rawOutput = document.querySelector('[data-raw]')
      const months = document.querySelector('#months')
      const dateOutputs = document.querySelectorAll('[data-date]')
      const options = {
        datePattern: ['Y', 'm', 'd'],
        delimiter: '-',
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'date', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const date = parseDate(formatted, { format: 'YYYY-MM-DD' })
        const moved = date && addDate(date, Number(months.value), 'month')
        const [added, end] = dateOutputs
        added.value = moved ? formatDatePattern(moved, 'YYYY-MM-DD') : 'Keep typing'
        end.value = moved
          ? formatDatePattern(endOfDate(moved, 'month'), 'YYYY-MM-DD')
          : 'Keep typing'
      }

      field.addEventListener('input', update)
      months.addEventListener('change', update)
      update()
    </script>
  </body>
</html>

Payment cards

Card number and brand

creditCard groups by brand, and creditCardType returns the brand name as formatted. Card formatting is not payment validation.

CardLive · formatted and raw

A card number with its brand

format(value, 'creditCard')

Grouping follows the detected card brand. The same digits drive a brand badge and a paired validation message.

Shown to people
4111 1111 1111 1111
Sent to your backend
4111111111111111
Brand
visa
regex.creditCard
Card number length is valid

creditCardType returns the brand as formatted and the digits as raw. Formatting is not payment validation: rely on your payment provider for that.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A card number with its brand</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="card-number">Card number</label>
        <input
          id="card-number"
          type="text"
          inputmode="numeric"
          autocomplete="cc-number"
          placeholder="0000 0000 0000 0000"
          value="4111111111111111"
          aria-describedby="card-number-raw card-number-status"
        />
        <input type="hidden" name="card_number" />
        <p id="card-number-raw">Sent to your backend: <output data-raw></output></p>
        <p>Brand: <output data-brand></output></p>
        <p id="card-number-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter

const field = document.querySelector('#card-number')
const hidden = document.querySelector('input[name="card_number"]')
const rawOutput = document.querySelector('[data-raw]')
const brand = document.querySelector('[data-brand]')
const status = document.querySelector('[data-status]')
const options = {}
const rule = regex.creditCard

const update = () => {
  const { formatted, raw } = format(field.value, 'creditCard', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  brand.value = format(raw, 'creditCardType').formatted || 'Unknown'
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Card number length is valid' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A card number with its brand</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="card-number">Card number</label>
        <input
          id="card-number"
          type="text"
          inputmode="numeric"
          autocomplete="cc-number"
          placeholder="0000 0000 0000 0000"
          value="4111111111111111"
          aria-describedby="card-number-raw card-number-status"
        />
        <input type="hidden" name="card_number" />
        <p id="card-number-raw">Sent to your backend: <output data-raw></output></p>
        <p>Brand: <output data-brand></output></p>
        <p id="card-number-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter

      const field = document.querySelector('#card-number')
      const hidden = document.querySelector('input[name="card_number"]')
      const rawOutput = document.querySelector('[data-raw]')
      const brand = document.querySelector('[data-brand]')
      const status = document.querySelector('[data-status]')
      const options = {}
      const rule = regex.creditCard

      const update = () => {
        const { formatted, raw } = format(field.value, 'creditCard', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        brand.value = format(raw, 'creditCardType').formatted || 'Unknown'
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Card number length is valid' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A card number with its brand</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="card-number">Card number</label>
        <input
          id="card-number"
          type="text"
          inputmode="numeric"
          autocomplete="cc-number"
          placeholder="0000 0000 0000 0000"
          value="4111111111111111"
          aria-describedby="card-number-raw card-number-status"
        />
        <input type="hidden" name="card_number" />
        <p id="card-number-raw">Sent to your backend: <output data-raw></output></p>
        <p>Brand: <output data-brand></output></p>
        <p id="card-number-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'

const field = document.querySelector('#card-number')
const hidden = document.querySelector('input[name="card_number"]')
const rawOutput = document.querySelector('[data-raw]')
const brand = document.querySelector('[data-brand]')
const status = document.querySelector('[data-status]')
const options = {}
const rule = regex.creditCard

const update = () => {
  const { formatted, raw } = format(field.value, 'creditCard', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  brand.value = format(raw, 'creditCardType').formatted || 'Unknown'
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Card number length is valid' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A card number with its brand</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="card-number">Card number</label>
        <input
          id="card-number"
          type="text"
          inputmode="numeric"
          autocomplete="cc-number"
          placeholder="0000 0000 0000 0000"
          value="4111111111111111"
          aria-describedby="card-number-raw card-number-status"
        />
        <input type="hidden" name="card_number" />
        <p id="card-number-raw">Sent to your backend: <output data-raw></output></p>
        <p>Brand: <output data-brand></output></p>
        <p id="card-number-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'

      const field = document.querySelector('#card-number')
      const hidden = document.querySelector('input[name="card_number"]')
      const rawOutput = document.querySelector('[data-raw]')
      const brand = document.querySelector('[data-brand]')
      const status = document.querySelector('[data-status]')
      const options = {}
      const rule = regex.creditCard

      const update = () => {
        const { formatted, raw } = format(field.value, 'creditCard', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        brand.value = format(raw, 'creditCardType').formatted || 'Unknown'
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Card number length is valid' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Custom masks

Locked prefix

The general type needs blocks. A locked prefix is displayed on every call, restored after a single deletion and excluded from raw unless rawPrefix: true.

MaskLive · formatted and raw

An order reference with a fixed prefix

format(value, 'general', { prefix, blocks, delimiter, numericOnly })

The ORD- prefix is locked, digits are grouped by blocks, and the backend receives only the digits. Deleting part of the prefix restores it.

Shown to people
ORD-2026-1105
Sent to your backend
20261105
regex.digits(8)
Complete reference

Pasted values that already include the prefix are accepted. Add rawPrefix: true when the backend stores ORD- as part of the value.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An order reference with a fixed prefix</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="order">Order reference</label>
        <input
          id="order"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="ORD-0000-0000"
          value="20261105"
          aria-describedby="order-raw order-status"
        />
        <input type="hidden" name="order_reference" />
        <p id="order-raw">Sent to your backend: <output data-raw></output></p>
        <p id="order-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter

const field = document.querySelector('#order')
const hidden = document.querySelector('input[name="order_reference"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  prefix: 'ORD-',
  blocks: [4, 4],
  delimiter: '-',
  numericOnly: true,
}
const rule = regex.digits(8)

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(raw)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Complete reference' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An order reference with a fixed prefix</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="order">Order reference</label>
        <input
          id="order"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="ORD-0000-0000"
          value="20261105"
          aria-describedby="order-raw order-status"
        />
        <input type="hidden" name="order_reference" />
        <p id="order-raw">Sent to your backend: <output data-raw></output></p>
        <p id="order-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter

      const field = document.querySelector('#order')
      const hidden = document.querySelector('input[name="order_reference"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        prefix: 'ORD-',
        blocks: [4, 4],
        delimiter: '-',
        numericOnly: true,
      }
      const rule = regex.digits(8)

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(raw)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Complete reference' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An order reference with a fixed prefix</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="order">Order reference</label>
        <input
          id="order"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="ORD-0000-0000"
          value="20261105"
          aria-describedby="order-raw order-status"
        />
        <input type="hidden" name="order_reference" />
        <p id="order-raw">Sent to your backend: <output data-raw></output></p>
        <p id="order-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'

const field = document.querySelector('#order')
const hidden = document.querySelector('input[name="order_reference"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  prefix: 'ORD-',
  blocks: [4, 4],
  delimiter: '-',
  numericOnly: true,
}
const rule = regex.digits(8)

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(raw)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Complete reference' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An order reference with a fixed prefix</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="order">Order reference</label>
        <input
          id="order"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="ORD-0000-0000"
          value="20261105"
          aria-describedby="order-raw order-status"
        />
        <input type="hidden" name="order_reference" />
        <p id="order-raw">Sent to your backend: <output data-raw></output></p>
        <p id="order-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'

      const field = document.querySelector('#order')
      const hidden = document.querySelector('input[name="order_reference"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        prefix: 'ORD-',
        blocks: [4, 4],
        delimiter: '-',
        numericOnly: true,
      }
      const rule = regex.digits(8)

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(raw)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Complete reference' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Unit suffix

suffix adds a trailing decoration. In the default lock mode, deleting part of it restores the full suffix.

WeightLive · formatted and raw

A measurement with a unit suffix

format(value, 'general', { blocks, numericOnly, suffix })

The unit stays at the end while people type digits. Backspace into the unit and it comes back; the backend receives only the number.

Shown to people
12 kg
Sent to your backend
12

suffix adds a trailing decoration to general values. The default lock mode restores it; suffixMode: "passthrough" lets people type it themselves.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A measurement with a unit suffix</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="weight">Package weight</label>
        <input
          id="weight"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="0 kg"
          value="12"
          aria-describedby="weight-raw"
        />
        <input type="hidden" name="weight_kg" />
        <p id="weight-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format } = window.Formatter

const field = document.querySelector('#weight')
const hidden = document.querySelector('input[name="weight_kg"]')
const rawOutput = document.querySelector('[data-raw]')
const options = {
  blocks: [5],
  numericOnly: true,
  suffix: ' kg',
}

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A measurement with a unit suffix</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="weight">Package weight</label>
        <input
          id="weight"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="0 kg"
          value="12"
          aria-describedby="weight-raw"
        />
        <input type="hidden" name="weight_kg" />
        <p id="weight-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format } = window.Formatter

      const field = document.querySelector('#weight')
      const hidden = document.querySelector('input[name="weight_kg"]')
      const rawOutput = document.querySelector('[data-raw]')
      const options = {
        blocks: [5],
        numericOnly: true,
        suffix: ' kg',
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A measurement with a unit suffix</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="weight">Package weight</label>
        <input
          id="weight"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="0 kg"
          value="12"
          aria-describedby="weight-raw"
        />
        <input type="hidden" name="weight_kg" />
        <p id="weight-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format } from '@samline/formatter'

const field = document.querySelector('#weight')
const hidden = document.querySelector('input[name="weight_kg"]')
const rawOutput = document.querySelector('[data-raw]')
const options = {
  blocks: [5],
  numericOnly: true,
  suffix: ' kg',
}

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A measurement with a unit suffix</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="weight">Package weight</label>
        <input
          id="weight"
          type="text"
          inputmode="numeric"
          autocomplete="off"
          placeholder="0 kg"
          value="12"
          aria-describedby="weight-raw"
        />
        <input type="hidden" name="weight_kg" />
        <p id="weight-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script type="module">
      import { format } from '@samline/formatter'

      const field = document.querySelector('#weight')
      const hidden = document.querySelector('input[name="weight_kg"]')
      const rawOutput = document.querySelector('[data-raw]')
      const options = {
        blocks: [5],
        numericOnly: true,
        suffix: ' kg',
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Passthrough prefix

prefixMode: 'passthrough' keeps the prefix people type, in any case when uppercase is set, instead of adding it for them.

Promo codeLive · formatted and raw

A promo code people type themselves

format(value, 'general', { blocks, prefix, prefixMode, uppercase })

Codes start with SAVE, but people often type it, in any case. Passthrough mode keeps every keystroke and uppercases the code.

Shown to people
SAVE20
Sent to your backend
20

prefixMode: "passthrough" recognizes the prefix as it is typed instead of adding it for the user. Raw excludes the prefix unless rawPrefix is true.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A promo code people type themselves</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="promo">Promo code</label>
        <input
          id="promo"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="SAVE20"
          value="save20"
          aria-describedby="promo-raw"
        />
        <input type="hidden" name="promo_code" />
        <p id="promo-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format } = window.Formatter

const field = document.querySelector('#promo')
const hidden = document.querySelector('input[name="promo_code"]')
const rawOutput = document.querySelector('[data-raw]')
const options = {
  blocks: [8],
  prefix: 'SAVE',
  prefixMode: 'passthrough',
  uppercase: true,
}

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A promo code people type themselves</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="promo">Promo code</label>
        <input
          id="promo"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="SAVE20"
          value="save20"
          aria-describedby="promo-raw"
        />
        <input type="hidden" name="promo_code" />
        <p id="promo-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format } = window.Formatter

      const field = document.querySelector('#promo')
      const hidden = document.querySelector('input[name="promo_code"]')
      const rawOutput = document.querySelector('[data-raw]')
      const options = {
        blocks: [8],
        prefix: 'SAVE',
        prefixMode: 'passthrough',
        uppercase: true,
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A promo code people type themselves</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="promo">Promo code</label>
        <input
          id="promo"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="SAVE20"
          value="save20"
          aria-describedby="promo-raw"
        />
        <input type="hidden" name="promo_code" />
        <p id="promo-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format } from '@samline/formatter'

const field = document.querySelector('#promo')
const hidden = document.querySelector('input[name="promo_code"]')
const rawOutput = document.querySelector('[data-raw]')
const options = {
  blocks: [8],
  prefix: 'SAVE',
  prefixMode: 'passthrough',
  uppercase: true,
}

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A promo code people type themselves</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="promo">Promo code</label>
        <input
          id="promo"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="SAVE20"
          value="save20"
          aria-describedby="promo-raw"
        />
        <input type="hidden" name="promo_code" />
        <p id="promo-raw">Sent to your backend: <output data-raw></output></p>
      </div>
    </main>

    <script type="module">
      import { format } from '@samline/formatter'

      const field = document.querySelector('#promo')
      const hidden = document.querySelector('input[name="promo_code"]')
      const rawOutput = document.querySelector('[data-raw]')
      const options = {
        blocks: [8],
        prefix: 'SAVE',
        prefixMode: 'passthrough',
        uppercase: true,
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

ZIP code with optional block

delimiterLazyShow: true shows the hyphen only when the second block starts. Pair it with regex.postalCode.

ZIP codeLive · formatted and raw

A US ZIP code with optional +4

format(value, 'general', { blocks, delimiter, numericOnly, delimiterLazyShow })

Five digits, with the hyphen shown only when the optional four extra digits start. Both forms pass the paired postal code rule.

Shown to people
90210
Sent to your backend
90210
regex.postalCode
Valid ZIP code

delimiterLazyShow waits for the next block before showing its delimiter, so a five-digit ZIP never ends with a dangling hyphen.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A US ZIP code with optional +4</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="zip">ZIP code</label>
        <input
          id="zip"
          type="text"
          inputmode="numeric"
          autocomplete="postal-code"
          placeholder="00000"
          value="90210"
          aria-describedby="zip-raw zip-status"
        />
        <input type="hidden" name="zip" />
        <p id="zip-raw">Sent to your backend: <output data-raw></output></p>
        <p id="zip-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter

const field = document.querySelector('#zip')
const hidden = document.querySelector('input[name="zip"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  blocks: [5, 4],
  delimiter: '-',
  numericOnly: true,
  delimiterLazyShow: true,
}
const rule = regex.postalCode

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid ZIP code' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A US ZIP code with optional +4</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="zip">ZIP code</label>
        <input
          id="zip"
          type="text"
          inputmode="numeric"
          autocomplete="postal-code"
          placeholder="00000"
          value="90210"
          aria-describedby="zip-raw zip-status"
        />
        <input type="hidden" name="zip" />
        <p id="zip-raw">Sent to your backend: <output data-raw></output></p>
        <p id="zip-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter

      const field = document.querySelector('#zip')
      const hidden = document.querySelector('input[name="zip"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        blocks: [5, 4],
        delimiter: '-',
        numericOnly: true,
        delimiterLazyShow: true,
      }
      const rule = regex.postalCode

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid ZIP code' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A US ZIP code with optional +4</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="zip">ZIP code</label>
        <input
          id="zip"
          type="text"
          inputmode="numeric"
          autocomplete="postal-code"
          placeholder="00000"
          value="90210"
          aria-describedby="zip-raw zip-status"
        />
        <input type="hidden" name="zip" />
        <p id="zip-raw">Sent to your backend: <output data-raw></output></p>
        <p id="zip-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'

const field = document.querySelector('#zip')
const hidden = document.querySelector('input[name="zip"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  blocks: [5, 4],
  delimiter: '-',
  numericOnly: true,
  delimiterLazyShow: true,
}
const rule = regex.postalCode

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(formatted)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid ZIP code' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A US ZIP code with optional +4</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="zip">ZIP code</label>
        <input
          id="zip"
          type="text"
          inputmode="numeric"
          autocomplete="postal-code"
          placeholder="00000"
          value="90210"
          aria-describedby="zip-raw zip-status"
        />
        <input type="hidden" name="zip" />
        <p id="zip-raw">Sent to your backend: <output data-raw></output></p>
        <p id="zip-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'

      const field = document.querySelector('#zip')
      const hidden = document.querySelector('input[name="zip"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        blocks: [5, 4],
        delimiter: '-',
        numericOnly: true,
        delimiterLazyShow: true,
      }
      const rule = regex.postalCode

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(formatted)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid ZIP code' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Postal code by country

Postal codes have no single shape, so this field uses the text type with case: 'upper' instead of a mask, and the country select builds the rule with regex.postalCode({ country }). 24 countries are built in; the static regex.postalCode stays the US ZIP, and other countries use regex.custom().

Postal codeLive · formatted and raw

A postal code checked for the selected country

format(value, 'text', { case })

Postal codes have no single shape: SW1A 1AA, K1A 0B1, 100-0001. The text type uppercases what people type and regex.postalCode({ country }) checks it for one of 24 countries.

Shown to people
SW1A 1AA
Sent to your backend
SW1A 1AA
regex.postalCode({ country })
Valid postal code

The static regex.postalCode stays the US ZIP. Countries without a built-in rule use regex.custom() with your own pattern.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A postal code checked for the selected country</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="postcode-country">Country</label>
        <select id="postcode-country">
          <option value="GB" selected>United Kingdom</option>
          <option value="CA">Canada</option>
          <option value="US">United States</option>
          <option value="MX">Mexico</option>
          <option value="DE">Germany</option>
          <option value="JP">Japan</option>
        </select>
        <label for="postcode">Postal code</label>
        <input
          id="postcode"
          type="text"
          inputmode="text"
          autocomplete="postal-code"
          placeholder="Postal code"
          value="sw1a 1aa"
          aria-describedby="postcode-raw postcode-status"
        />
        <input type="hidden" name="postcode" />
        <p id="postcode-raw">Sent to your backend: <output data-raw></output></p>
        <p id="postcode-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter

const field = document.querySelector('#postcode')
const hidden = document.querySelector('input[name="postcode"]')
const rawOutput = document.querySelector('[data-raw]')
const country = document.querySelector('#postcode-country')
const status = document.querySelector('[data-status]')
const options = {
  case: 'upper',
}

const update = () => {
  const { formatted, raw } = format(field.value, 'text', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const rule = regex.postalCode({ country: country.value })
  const valid = rule.pattern.test(raw)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid postal code' : rule.errorMessage
}

field.addEventListener('input', update)
country.addEventListener('change', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A postal code checked for the selected country</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="postcode-country">Country</label>
        <select id="postcode-country">
          <option value="GB" selected>United Kingdom</option>
          <option value="CA">Canada</option>
          <option value="US">United States</option>
          <option value="MX">Mexico</option>
          <option value="DE">Germany</option>
          <option value="JP">Japan</option>
        </select>
        <label for="postcode">Postal code</label>
        <input
          id="postcode"
          type="text"
          inputmode="text"
          autocomplete="postal-code"
          placeholder="Postal code"
          value="sw1a 1aa"
          aria-describedby="postcode-raw postcode-status"
        />
        <input type="hidden" name="postcode" />
        <p id="postcode-raw">Sent to your backend: <output data-raw></output></p>
        <p id="postcode-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter

      const field = document.querySelector('#postcode')
      const hidden = document.querySelector('input[name="postcode"]')
      const rawOutput = document.querySelector('[data-raw]')
      const country = document.querySelector('#postcode-country')
      const status = document.querySelector('[data-status]')
      const options = {
        case: 'upper',
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'text', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const rule = regex.postalCode({ country: country.value })
        const valid = rule.pattern.test(raw)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid postal code' : rule.errorMessage
      }

      field.addEventListener('input', update)
      country.addEventListener('change', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A postal code checked for the selected country</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="postcode-country">Country</label>
        <select id="postcode-country">
          <option value="GB" selected>United Kingdom</option>
          <option value="CA">Canada</option>
          <option value="US">United States</option>
          <option value="MX">Mexico</option>
          <option value="DE">Germany</option>
          <option value="JP">Japan</option>
        </select>
        <label for="postcode">Postal code</label>
        <input
          id="postcode"
          type="text"
          inputmode="text"
          autocomplete="postal-code"
          placeholder="Postal code"
          value="sw1a 1aa"
          aria-describedby="postcode-raw postcode-status"
        />
        <input type="hidden" name="postcode" />
        <p id="postcode-raw">Sent to your backend: <output data-raw></output></p>
        <p id="postcode-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'

const field = document.querySelector('#postcode')
const hidden = document.querySelector('input[name="postcode"]')
const rawOutput = document.querySelector('[data-raw]')
const country = document.querySelector('#postcode-country')
const status = document.querySelector('[data-status]')
const options = {
  case: 'upper',
}

const update = () => {
  const { formatted, raw } = format(field.value, 'text', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const rule = regex.postalCode({ country: country.value })
  const valid = rule.pattern.test(raw)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid postal code' : rule.errorMessage
}

field.addEventListener('input', update)
country.addEventListener('change', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A postal code checked for the selected country</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="postcode-country">Country</label>
        <select id="postcode-country">
          <option value="GB" selected>United Kingdom</option>
          <option value="CA">Canada</option>
          <option value="US">United States</option>
          <option value="MX">Mexico</option>
          <option value="DE">Germany</option>
          <option value="JP">Japan</option>
        </select>
        <label for="postcode">Postal code</label>
        <input
          id="postcode"
          type="text"
          inputmode="text"
          autocomplete="postal-code"
          placeholder="Postal code"
          value="sw1a 1aa"
          aria-describedby="postcode-raw postcode-status"
        />
        <input type="hidden" name="postcode" />
        <p id="postcode-raw">Sent to your backend: <output data-raw></output></p>
        <p id="postcode-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'

      const field = document.querySelector('#postcode')
      const hidden = document.querySelector('input[name="postcode"]')
      const rawOutput = document.querySelector('[data-raw]')
      const country = document.querySelector('#postcode-country')
      const status = document.querySelector('[data-status]')
      const options = {
        case: 'upper',
      }

      const update = () => {
        const { formatted, raw } = format(field.value, 'text', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const rule = regex.postalCode({ country: country.value })
        const valid = rule.pattern.test(raw)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid postal code' : rule.errorMessage
      }

      field.addEventListener('input', update)
      country.addEventListener('change', update)
      update()
    </script>
  </body>
</html>

Uppercase identifier

uppercase: true transforms the display, and raw follows it because there is no decoration. regex.curp checks the structure (date, sex, state and inner consonants) and isValidCurp the final check digit, so Wrong check digit passes the pattern and still fails. The CDN file reads isValidCurp from window.Formatter.checks.

CURPLive · formatted and raw

A Mexican CURP in capital letters

format(value, 'general', { blocks, uppercase })

An 18-character identifier typed in any case. The display and raw value are uppercase; regex.curp checks the structure and isValidCurp the final check digit.

Shown to people
GOMA800101HDFRRN05
Sent to your backend
GOMA800101HDFRRN05
regex.curp + isValidCurp
Valid CURP and check digit

uppercase transforms the display; without a prefix or suffix, raw is derived from the formatted value. A pattern cannot see a typo in a digit; the check digit can.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A Mexican CURP in capital letters</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="curp">CURP</label>
        <input
          id="curp"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="AAAA000000AAAAAA00"
          value="goma800101hdfrrn05"
          aria-describedby="curp-raw curp-status"
        />
        <input type="hidden" name="curp" />
        <p id="curp-raw">Sent to your backend: <output data-raw></output></p>
        <p id="curp-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter
const { isValidCurp } = window.Formatter.checks

const field = document.querySelector('#curp')
const hidden = document.querySelector('input[name="curp"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  blocks: [18],
  uppercase: true,
}
const rule = regex.curp

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const shape = rule.pattern.test(raw)
  const valid = shape && isValidCurp(raw)
  status.dataset.valid = String(valid)
  status.textContent = !shape
    ? rule.errorMessage
    : valid
      ? 'Valid CURP and check digit'
      : 'The CURP check digit does not match.'
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A Mexican CURP in capital letters</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="curp">CURP</label>
        <input
          id="curp"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="AAAA000000AAAAAA00"
          value="goma800101hdfrrn05"
          aria-describedby="curp-raw curp-status"
        />
        <input type="hidden" name="curp" />
        <p id="curp-raw">Sent to your backend: <output data-raw></output></p>
        <p id="curp-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter
      const { isValidCurp } = window.Formatter.checks

      const field = document.querySelector('#curp')
      const hidden = document.querySelector('input[name="curp"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        blocks: [18],
        uppercase: true,
      }
      const rule = regex.curp

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const shape = rule.pattern.test(raw)
        const valid = shape && isValidCurp(raw)
        status.dataset.valid = String(valid)
        status.textContent = !shape
          ? rule.errorMessage
          : valid
            ? 'Valid CURP and check digit'
            : 'The CURP check digit does not match.'
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A Mexican CURP in capital letters</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="curp">CURP</label>
        <input
          id="curp"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="AAAA000000AAAAAA00"
          value="goma800101hdfrrn05"
          aria-describedby="curp-raw curp-status"
        />
        <input type="hidden" name="curp" />
        <p id="curp-raw">Sent to your backend: <output data-raw></output></p>
        <p id="curp-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, isValidCurp, regex } from '@samline/formatter'

const field = document.querySelector('#curp')
const hidden = document.querySelector('input[name="curp"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  blocks: [18],
  uppercase: true,
}
const rule = regex.curp

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const shape = rule.pattern.test(raw)
  const valid = shape && isValidCurp(raw)
  status.dataset.valid = String(valid)
  status.textContent = !shape
    ? rule.errorMessage
    : valid
      ? 'Valid CURP and check digit'
      : 'The CURP check digit does not match.'
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A Mexican CURP in capital letters</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="curp">CURP</label>
        <input
          id="curp"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="AAAA000000AAAAAA00"
          value="goma800101hdfrrn05"
          aria-describedby="curp-raw curp-status"
        />
        <input type="hidden" name="curp" />
        <p id="curp-raw">Sent to your backend: <output data-raw></output></p>
        <p id="curp-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, isValidCurp, regex } from '@samline/formatter'

      const field = document.querySelector('#curp')
      const hidden = document.querySelector('input[name="curp"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        blocks: [18],
        uppercase: true,
      }
      const rule = regex.curp

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const shape = rule.pattern.test(raw)
        const valid = shape && isValidCurp(raw)
        status.dataset.valid = String(valid)
        status.textContent = !shape
          ? rule.errorMessage
          : valid
            ? 'Valid CURP and check digit'
            : 'The CURP check digit does not match.'
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Prefix kept in raw

rawPrefix: true stores the # with the value, so the backend receives a complete CSS color.

Hex colorLive · formatted and raw

A brand color with the # included

format(value, 'general', { prefix, blocks, uppercase, rawPrefix })

The # is shown and stored, letters are uppercased and the value is capped at six characters. The paired rule accepts #RGB and #RRGGBB.

Shown to people
#FF8800
Sent to your backend
#FF8800
regex.hexColor
Valid hex color

rawPrefix: true keeps the prefix in raw, so the stored value is a complete CSS color. Without it raw would be FF8800.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A brand color with the # included</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="brand-color">Brand color</label>
        <input
          id="brand-color"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="#000000"
          value="ff8800"
          aria-describedby="brand-color-raw brand-color-status"
        />
        <input type="hidden" name="brand_color" />
        <p id="brand-color-raw">Sent to your backend: <output data-raw></output></p>
        <p id="brand-color-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter

const field = document.querySelector('#brand-color')
const hidden = document.querySelector('input[name="brand_color"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  prefix: '#',
  blocks: [6],
  uppercase: true,
  rawPrefix: true,
}
const rule = regex.hexColor

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(raw)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid hex color' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A brand color with the # included</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="brand-color">Brand color</label>
        <input
          id="brand-color"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="#000000"
          value="ff8800"
          aria-describedby="brand-color-raw brand-color-status"
        />
        <input type="hidden" name="brand_color" />
        <p id="brand-color-raw">Sent to your backend: <output data-raw></output></p>
        <p id="brand-color-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter

      const field = document.querySelector('#brand-color')
      const hidden = document.querySelector('input[name="brand_color"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        prefix: '#',
        blocks: [6],
        uppercase: true,
        rawPrefix: true,
      }
      const rule = regex.hexColor

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(raw)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid hex color' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A brand color with the # included</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="brand-color">Brand color</label>
        <input
          id="brand-color"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="#000000"
          value="ff8800"
          aria-describedby="brand-color-raw brand-color-status"
        />
        <input type="hidden" name="brand_color" />
        <p id="brand-color-raw">Sent to your backend: <output data-raw></output></p>
        <p id="brand-color-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, regex } from '@samline/formatter'

const field = document.querySelector('#brand-color')
const hidden = document.querySelector('input[name="brand_color"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  prefix: '#',
  blocks: [6],
  uppercase: true,
  rawPrefix: true,
}
const rule = regex.hexColor

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const valid = rule.pattern.test(raw)
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Valid hex color' : rule.errorMessage
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A brand color with the # included</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="brand-color">Brand color</label>
        <input
          id="brand-color"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="#000000"
          value="ff8800"
          aria-describedby="brand-color-raw brand-color-status"
        />
        <input type="hidden" name="brand_color" />
        <p id="brand-color-raw">Sent to your backend: <output data-raw></output></p>
        <p id="brand-color-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, regex } from '@samline/formatter'

      const field = document.querySelector('#brand-color')
      const hidden = document.querySelector('input[name="brand_color"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        prefix: '#',
        blocks: [6],
        uppercase: true,
        rawPrefix: true,
      }
      const rule = regex.hexColor

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const valid = rule.pattern.test(raw)
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Valid hex color' : rule.errorMessage
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Validation

IBAN with check digits

The mask groups the IBAN in fours as it is printed, and raw drops the spaces. regex.iban checks the shape; isValidIban checks the length for the country and the mod 97 check digits, so One digit wrong fails although its shape is valid. The check-digit validators ignore spaces and hyphens and do not import regex.

IBANLive · formatted and raw

An IBAN grouped in fours and checked with mod 97

format(value, 'general', { blocks, delimiter, uppercase })

The mask uppercases and groups the IBAN as it is printed. regex.iban checks the shape; isValidIban checks the length for the country and the two check digits, so a mistyped character fails.

Shown to people
GB82 WEST 1234 5698 7654 32
Sent to your backend
GB82WEST12345698765432
regex.iban + isValidIban
Valid IBAN and check digits

raw drops the spaces. The check-digit validators ignore spaces and hyphens, return a boolean and do not import regex.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An IBAN grouped in fours and checked with mod 97</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="iban">IBAN</label>
        <input
          id="iban"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="GB00 0000 0000 0000 0000 00"
          value="gb82west12345698765432"
          aria-describedby="iban-raw iban-status"
        />
        <input type="hidden" name="iban" />
        <p id="iban-raw">Sent to your backend: <output data-raw></output></p>
        <p id="iban-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { format, regex } = window.Formatter
const { isValidIban } = window.Formatter.checks

const field = document.querySelector('#iban')
const hidden = document.querySelector('input[name="iban"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  blocks: [4, 4, 4, 4, 4, 4, 4, 4, 2],
  delimiter: ' ',
  uppercase: true,
}
const rule = regex.iban

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const shape = rule.pattern.test(raw)
  const valid = shape && isValidIban(raw)
  status.dataset.valid = String(valid)
  status.textContent = !shape
    ? rule.errorMessage
    : valid
      ? 'Valid IBAN and check digits'
      : 'One character is wrong: the IBAN check digits do not match.'
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An IBAN grouped in fours and checked with mod 97</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="iban">IBAN</label>
        <input
          id="iban"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="GB00 0000 0000 0000 0000 00"
          value="gb82west12345698765432"
          aria-describedby="iban-raw iban-status"
        />
        <input type="hidden" name="iban" />
        <p id="iban-raw">Sent to your backend: <output data-raw></output></p>
        <p id="iban-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { format, regex } = window.Formatter
      const { isValidIban } = window.Formatter.checks

      const field = document.querySelector('#iban')
      const hidden = document.querySelector('input[name="iban"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        blocks: [4, 4, 4, 4, 4, 4, 4, 4, 2],
        delimiter: ' ',
        uppercase: true,
      }
      const rule = regex.iban

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const shape = rule.pattern.test(raw)
        const valid = shape && isValidIban(raw)
        status.dataset.valid = String(valid)
        status.textContent = !shape
          ? rule.errorMessage
          : valid
            ? 'Valid IBAN and check digits'
            : 'One character is wrong: the IBAN check digits do not match.'
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An IBAN grouped in fours and checked with mod 97</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="iban">IBAN</label>
        <input
          id="iban"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="GB00 0000 0000 0000 0000 00"
          value="gb82west12345698765432"
          aria-describedby="iban-raw iban-status"
        />
        <input type="hidden" name="iban" />
        <p id="iban-raw">Sent to your backend: <output data-raw></output></p>
        <p id="iban-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { format, isValidIban, regex } from '@samline/formatter'

const field = document.querySelector('#iban')
const hidden = document.querySelector('input[name="iban"]')
const rawOutput = document.querySelector('[data-raw]')
const status = document.querySelector('[data-status]')
const options = {
  blocks: [4, 4, 4, 4, 4, 4, 4, 4, 2],
  delimiter: ' ',
  uppercase: true,
}
const rule = regex.iban

const update = () => {
  const { formatted, raw } = format(field.value, 'general', options)
  field.value = formatted
  hidden.value = raw
  rawOutput.value = raw || '—'
  const shape = rule.pattern.test(raw)
  const valid = shape && isValidIban(raw)
  status.dataset.valid = String(valid)
  status.textContent = !shape
    ? rule.errorMessage
    : valid
      ? 'Valid IBAN and check digits'
      : 'One character is wrong: the IBAN check digits do not match.'
}

field.addEventListener('input', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An IBAN grouped in fours and checked with mod 97</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="iban">IBAN</label>
        <input
          id="iban"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="GB00 0000 0000 0000 0000 00"
          value="gb82west12345698765432"
          aria-describedby="iban-raw iban-status"
        />
        <input type="hidden" name="iban" />
        <p id="iban-raw">Sent to your backend: <output data-raw></output></p>
        <p id="iban-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { format, isValidIban, regex } from '@samline/formatter'

      const field = document.querySelector('#iban')
      const hidden = document.querySelector('input[name="iban"]')
      const rawOutput = document.querySelector('[data-raw]')
      const status = document.querySelector('[data-status]')
      const options = {
        blocks: [4, 4, 4, 4, 4, 4, 4, 4, 2],
        delimiter: ' ',
        uppercase: true,
      }
      const rule = regex.iban

      const update = () => {
        const { formatted, raw } = format(field.value, 'general', options)
        field.value = formatted
        hidden.value = raw
        rawOutput.value = raw || '—'
        const shape = rule.pattern.test(raw)
        const valid = shape && isValidIban(raw)
        status.dataset.valid = String(valid)
        status.textContent = !shape
          ? rule.errorMessage
          : valid
            ? 'Valid IBAN and check digits'
            : 'One character is wrong: the IBAN check digits do not match.'
      }

      field.addEventListener('input', update)
      update()
    </script>
  </body>
</html>

Validation rules

Pick a rule from the regex catalog. Static entries are records; parametric entries such as password() are called first. Every rule pairs its pattern with its message. regex.phone allows spaces, hyphens, dots, parentheses and + between the digits, but no letters: Letters between digits (abc5512345678) fails since 3.2.0.

RulesLive · pattern and message

Validation rules with ready messages

regex[rule].pattern.test(value)

Choose a rule from the regex catalog and type a value. Each rule pairs its pattern with the message to show, so the two never drift apart.

Pattern
^(?=.{1,254}$)(?=[^@]{1,64}@)[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[\p{L}\p{N}](?:[\p{L}\p{N}-]{0,61}[\p{L}\p{N}])?\.)+(?:\p{L}{2,63}|xn--[a-zA-Z0-9-]{1,59})$
Result
Looks valid

Static rules are records; parametric ones such as password() are called first. Validate again on the server: these patterns give quick feedback.

Explore the code

HTML / CDN

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Validation rules with ready messages</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="rule">Rule</label>
        <select id="rule">
          <option value="email" selected>Email address</option>
          <option value="password">Password strength</option>
          <option value="url">URL</option>
          <option value="rfc">Mexican RFC</option>
          <option value="curp">Mexican CURP</option>
          <option value="personName">Person name</option>
          <option value="phone">Phone number (10 digits)</option>
          <option value="ine">Mexican INE voter key</option>
          <option value="ssn">US Social Security number</option>
          <option value="iban">IBAN</option>
          <option value="ipv4">IPv4 address</option>
          <option value="uuid">UUID</option>
          <option value="slug">URL slug</option>
          <option value="username">Username</option>
          <option value="semver">Semantic version</option>
          <option value="macAddress">MAC address</option>
        </select>
        <label for="value">Value to check</label>
        <input
          id="value"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="Type a value"
          value="hello@example.com"
          aria-describedby="value-status"
        />
        <p>Pattern: <output data-pattern></output></p>
        <p id="value-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script src="./main.js" defer></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
const { regex } = window.Formatter

const field = document.querySelector('#value')
const ruleSelect = document.querySelector('#rule')
const patternOutput = document.querySelector('[data-pattern]')
const status = document.querySelector('[data-status]')

const update = () => {
  // Parametric entries such as password() are called for their default rule.
  const entry = regex[ruleSelect.value]
  const rule = typeof entry === 'function' ? entry() : entry
  const valid = rule.pattern.test(field.value)
  patternOutput.value = rule.pattern.source
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Looks valid' : rule.errorMessage
}

field.addEventListener('input', update)
ruleSelect.addEventListener('change', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Validation rules with ready messages</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="rule">Rule</label>
        <select id="rule">
          <option value="email" selected>Email address</option>
          <option value="password">Password strength</option>
          <option value="url">URL</option>
          <option value="rfc">Mexican RFC</option>
          <option value="curp">Mexican CURP</option>
          <option value="personName">Person name</option>
          <option value="phone">Phone number (10 digits)</option>
          <option value="ine">Mexican INE voter key</option>
          <option value="ssn">US Social Security number</option>
          <option value="iban">IBAN</option>
          <option value="ipv4">IPv4 address</option>
          <option value="uuid">UUID</option>
          <option value="slug">URL slug</option>
          <option value="username">Username</option>
          <option value="semver">Semantic version</option>
          <option value="macAddress">MAC address</option>
        </select>
        <label for="value">Value to check</label>
        <input
          id="value"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="Type a value"
          value="hello@example.com"
          aria-describedby="value-status"
        />
        <p>Pattern: <output data-pattern></output></p>
        <p id="value-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script src="https://cdn.jsdelivr.net/npm/@samline/formatter@3.3.1/dist/browser/formatter.global.js"></script>
    <script>
      const { regex } = window.Formatter

      const field = document.querySelector('#value')
      const ruleSelect = document.querySelector('#rule')
      const patternOutput = document.querySelector('[data-pattern]')
      const status = document.querySelector('[data-status]')

      const update = () => {
        // Parametric entries such as password() are called for their default rule.
        const entry = regex[ruleSelect.value]
        const rule = typeof entry === 'function' ? entry() : entry
        const valid = rule.pattern.test(field.value)
        patternOutput.value = rule.pattern.source
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Looks valid' : rule.errorMessage
      }

      field.addEventListener('input', update)
      ruleSelect.addEventListener('change', update)
      update()
    </script>
  </body>
</html>

Package / ESM

HTML

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Validation rules with ready messages</title>
    <link rel="stylesheet" href="./style.css" />
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="rule">Rule</label>
        <select id="rule">
          <option value="email" selected>Email address</option>
          <option value="password">Password strength</option>
          <option value="url">URL</option>
          <option value="rfc">Mexican RFC</option>
          <option value="curp">Mexican CURP</option>
          <option value="personName">Person name</option>
          <option value="phone">Phone number (10 digits)</option>
          <option value="ine">Mexican INE voter key</option>
          <option value="ssn">US Social Security number</option>
          <option value="iban">IBAN</option>
          <option value="ipv4">IPv4 address</option>
          <option value="uuid">UUID</option>
          <option value="slug">URL slug</option>
          <option value="username">Username</option>
          <option value="semver">Semantic version</option>
          <option value="macAddress">MAC address</option>
        </select>
        <label for="value">Value to check</label>
        <input
          id="value"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="Type a value"
          value="hello@example.com"
          aria-describedby="value-status"
        />
        <p>Pattern: <output data-pattern></output></p>
        <p id="value-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module" src="./main.js"></script>
  </body>
</html>

CSS

style.css
.formatter-field {
  display: grid;
  gap: 0.5rem;
  max-width: 24rem;
  padding: 1.5rem;
  border: 0.0625rem solid #c9d3b8;
  border-radius: 1rem;
  font:
    1rem/1.6 system-ui,
    sans-serif;
}
.formatter-field label {
  font-weight: 600;
}
.formatter-field input,
.formatter-field select {
  padding: 0.75rem 0.875rem;
  border: 0.0625rem solid #9aa58a;
  border-radius: 0.5rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
}
.formatter-field select {
  cursor: pointer;
}
.formatter-field input:focus-visible,
.formatter-field select:focus-visible {
  outline: 0.125rem solid #4d7c0f;
  outline-offset: 0.125rem;
}
.formatter-field p {
  margin: 0;
  color: #4a5240;
}
.formatter-field output {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
}
.formatter-field [data-status][data-valid='false'] {
  color: #9b2c2c;
}

JavaScript

main.js
import { regex } from '@samline/formatter'

const field = document.querySelector('#value')
const ruleSelect = document.querySelector('#rule')
const patternOutput = document.querySelector('[data-pattern]')
const status = document.querySelector('[data-status]')

const update = () => {
  // Parametric entries such as password() are called for their default rule.
  const entry = regex[ruleSelect.value]
  const rule = typeof entry === 'function' ? entry() : entry
  const valid = rule.pattern.test(field.value)
  patternOutput.value = rule.pattern.source
  status.dataset.valid = String(valid)
  status.textContent = valid ? 'Looks valid' : rule.errorMessage
}

field.addEventListener('input', update)
ruleSelect.addEventListener('change', update)
update()

Complete file

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Validation rules with ready messages</title>
    <style>
      .formatter-field {
        display: grid;
        gap: 0.5rem;
        max-width: 24rem;
        padding: 1.5rem;
        border: 0.0625rem solid #c9d3b8;
        border-radius: 1rem;
        font:
          1rem/1.6 system-ui,
          sans-serif;
      }
      .formatter-field label {
        font-weight: 600;
      }
      .formatter-field input,
      .formatter-field select {
        padding: 0.75rem 0.875rem;
        border: 0.0625rem solid #9aa58a;
        border-radius: 0.5rem;
        font: inherit;
        font-variant-numeric: tabular-nums;
      }
      .formatter-field select {
        cursor: pointer;
      }
      .formatter-field input:focus-visible,
      .formatter-field select:focus-visible {
        outline: 0.125rem solid #4d7c0f;
        outline-offset: 0.125rem;
      }
      .formatter-field p {
        margin: 0;
        color: #4a5240;
      }
      .formatter-field output {
        font-family: ui-monospace, monospace;
        overflow-wrap: anywhere;
      }
      .formatter-field [data-status][data-valid='false'] {
        color: #9b2c2c;
      }
    </style>
  </head>

  <body>
    <main>
      <div class="formatter-field">
        <label for="rule">Rule</label>
        <select id="rule">
          <option value="email" selected>Email address</option>
          <option value="password">Password strength</option>
          <option value="url">URL</option>
          <option value="rfc">Mexican RFC</option>
          <option value="curp">Mexican CURP</option>
          <option value="personName">Person name</option>
          <option value="phone">Phone number (10 digits)</option>
          <option value="ine">Mexican INE voter key</option>
          <option value="ssn">US Social Security number</option>
          <option value="iban">IBAN</option>
          <option value="ipv4">IPv4 address</option>
          <option value="uuid">UUID</option>
          <option value="slug">URL slug</option>
          <option value="username">Username</option>
          <option value="semver">Semantic version</option>
          <option value="macAddress">MAC address</option>
        </select>
        <label for="value">Value to check</label>
        <input
          id="value"
          type="text"
          inputmode="text"
          autocomplete="off"
          placeholder="Type a value"
          value="hello@example.com"
          aria-describedby="value-status"
        />
        <p>Pattern: <output data-pattern></output></p>
        <p id="value-status" data-status aria-live="polite"></p>
      </div>
    </main>

    <script type="module">
      import { regex } from '@samline/formatter'

      const field = document.querySelector('#value')
      const ruleSelect = document.querySelector('#rule')
      const patternOutput = document.querySelector('[data-pattern]')
      const status = document.querySelector('[data-status]')

      const update = () => {
        // Parametric entries such as password() are called for their default rule.
        const entry = regex[ruleSelect.value]
        const rule = typeof entry === 'function' ? entry() : entry
        const valid = rule.pattern.test(field.value)
        patternOutput.value = rule.pattern.source
        status.dataset.valid = String(valid)
        status.textContent = valid ? 'Looks valid' : rule.errorMessage
      }

      field.addEventListener('input', update)
      ruleSelect.addEventListener('change', update)
      update()
    </script>
  </body>
</html>