Maska: Input Mask

3.2.0 · active · verified Sun Apr 19

Maska is a lightweight, zero-dependency JavaScript library for creating input masks, supporting Vanilla JS, Vue (2 and 3), Alpine.js, and Svelte. The current stable version is 3.2.0, with ongoing maintenance and minor releases following a significant v3.0.0 rewrite. Key differentiators include its minimal footprint (~3 Kb gzipped), comprehensive framework integrations, and advanced masking features such as custom tokens with modifiers and transform functions, hooks, and a dedicated number mask mode for simplified currency and numerical formatting. It also supports dynamic, reversed, and eager masks, providing robust solutions for various input validation scenarios.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic Maska usage for Vanilla JavaScript, applying a phone number mask and a reversed currency mask to input fields. It shows how to initialize Maska, define masks and tokens, and access masked/unmasked values and completion status.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Maska Quickstart</title>
</head>
<body>
  <label for="phone-input">Phone Number (US):</label>
  <input type="text" id="phone-input" placeholder="+1 (___) ___-____" />

  <label for="currency-input">Currency ($):</label>
  <input type="text" id="currency-input" placeholder="$__.__" />

  <script type="module">
    import { Maska } from 'maska';

    document.addEventListener('DOMContentLoaded', () => {
      // Example 1: Phone number mask
      const phoneInput = document.getElementById('phone-input');
      const phoneMask = new Maska(phoneInput, {
        mask: '+1 (###) ###-####',
        tokens: {
          '#': { pattern: /\d/ }
        }
      });

      phoneInput.addEventListener('input', () => {
        console.log('Phone - Unmasked:', phoneMask.unmasked, 'Completed:', phoneMask.completed);
      });

      // Example 2: Currency mask
      const currencyInput = document.getElementById('currency-input');
      const currencyMask = new Maska(currencyInput, {
        mask: '$###,###,###.##', // Example for up to millions, two decimal places
        reversed: true,
        tokens: {
          '#': { pattern: /\d/, multiple: true }
        }
      });

      currencyInput.addEventListener('input', () => {
        console.log('Currency - Masked:', currencyMask.masked, 'Unmasked:', currencyMask.unmasked);
      });
    });
  </script>
</body>
</html>

view raw JSON →