Convert Figma logo to code with AI

beholdr logomaska

Simple zero-dependency input mask for Vue, Svelte, Alpine.js and vanilla JS.

1,798
71
1,798
4

Top Related Projects

4,979

vanilla javascript input mask

Input Mask plugin

17,957

Format input text content when you are typing...

Input mask for React, Angular, Ember, Vue, & plain JavaScript

A jQuery Plugin to make masks on form fields and HTML elements.

VanillaMasker is a pure javascript mask input

Quick Overview

Maska is a lightweight and flexible input mask library for JavaScript. It provides a simple and intuitive way to format user input, ensuring that the data entered follows a specific pattern or format.

Pros

  • Lightweight: Maska is a small library, weighing in at around 3KB (gzipped), making it suitable for use in performance-sensitive applications.
  • Flexible: The library supports a wide range of input formats, including numeric, alphanumeric, and custom patterns, allowing developers to tailor the input experience to their specific needs.
  • Customizable: Maska offers a range of configuration options, enabling developers to fine-tune the behavior of the input mask to match their application's requirements.
  • Framework-agnostic: Maska can be used with any JavaScript-based framework or library, making it a versatile choice for a variety of web development projects.

Cons

  • Limited Functionality: While Maska provides a solid set of features, it may not offer the same level of advanced functionality as some other input mask libraries, such as support for dynamic masks or complex validation rules.
  • Dependency on Vanilla JavaScript: Maska is primarily designed for use with vanilla JavaScript, and while it can be integrated with frameworks, the integration process may require additional effort.
  • Potential Performance Issues: Depending on the complexity of the input mask and the number of elements using it, Maska may have some performance implications, especially in large-scale applications.
  • Lack of Extensive Documentation: The project's documentation, while generally clear and concise, may not provide as much detailed information and examples as some developers might prefer.

Code Examples

Here are a few examples of how to use Maska in your JavaScript projects:

  1. Applying a Numeric Mask:
import Maska from 'maska';

const input = document.querySelector('input');
Maska(input, { mask: '##-##-##' });

This code applies a numeric mask to the input field, ensuring that the user can only enter a pattern of two digits, a hyphen, two digits, a hyphen, and two more digits.

  1. Applying a Custom Mask:
import Maska from 'maska';

const input = document.querySelector('input');
Maska(input, { mask: '(###) ###-####' });

This example applies a custom mask to the input field, formatting the user's input as a US phone number.

  1. Applying a Mask with Dynamic Placeholders:
import Maska from 'maska';

const input = document.querySelector('input');
Maska(input, { mask: 'AA-##-##', tokens: { 'A': /[a-zA-Z]/, '#': /\d/ } });

In this example, the mask includes both alphabetic and numeric characters, with the tokens defining the allowed characters for each placeholder.

  1. Applying a Mask with a Custom Validation Function:
import Maska from 'maska';

const input = document.querySelector('input');
Maska(input, {
  mask: '##-##-##',
  validate: (value) => /^\d{2}-\d{2}-\d{2}$/.test(value)
});

This code applies a numeric mask to the input field and also includes a custom validation function to ensure that the entered value matches the expected pattern.

Getting Started

To get started with Maska, follow these steps:

  1. Install the library using npm or yarn:
npm install maska

or

yarn add maska
  1. Import the Maska library in your JavaScript file:
import Maska from 'maska';
  1. Apply a mask to an input field:
const input = document.querySelector('input');
Maska(input, { mask: '##-##-##' });
  1. Customize the mask as needed, using the available options:
Maska(input, {
  mask: '(###) ###-####',
  tokens: {
    '#': /\d/,
    'X': /[a-zA-Z]/
  },
  validate: (value) => /

Competitor Comparisons

4,979

vanilla javascript input mask

Pros of imaskjs

  • More comprehensive and feature-rich, offering a wider range of masking options
  • Better documentation and examples, making it easier for developers to implement
  • Supports both vanilla JavaScript and various frameworks (React, Vue, Angular)

Cons of imaskjs

  • Larger file size, which may impact page load times
  • More complex API, potentially requiring a steeper learning curve
  • Higher memory usage due to its extensive feature set

Code Comparison

maska:

import Maska from 'maska'

Maska.create('#phone', {
  mask: '(###) ###-####'
})

imaskjs:

import IMask from 'imask'

IMask(document.getElementById('phone'), {
  mask: '(000) 000-0000'
})

Summary

imaskjs offers more features and flexibility, making it suitable for complex masking requirements across various frameworks. However, this comes at the cost of a larger file size and potentially more complex implementation. maska, on the other hand, provides a simpler, lightweight solution that may be sufficient for basic masking needs. The choice between the two depends on the specific requirements of your project and the level of customization needed.

Input Mask plugin

Pros of Inputmask

  • More extensive feature set, including support for various input types and complex masking scenarios
  • Larger community and longer development history, potentially leading to better stability and support
  • Offers more customization options and flexibility for advanced use cases

Cons of Inputmask

  • Larger file size and potentially higher performance overhead
  • Steeper learning curve due to more complex API and configuration options
  • May be overkill for simple masking requirements

Code Comparison

Maska:

import Maska from 'maska'

Maska.create('#phone', {
  mask: '(###) ###-####'
})

Inputmask:

import Inputmask from 'inputmask'

Inputmask({
  mask: '(999) 999-9999'
}).mask('#phone')

Both libraries offer similar basic functionality for input masking, but Inputmask provides more advanced options for complex scenarios. Maska's syntax is generally simpler and more straightforward for basic use cases, while Inputmask offers greater flexibility at the cost of increased complexity.

The choice between these libraries depends on the specific requirements of your project, with Maska being suitable for simpler masking needs and Inputmask better suited for more complex and diverse input masking scenarios.

17,957

Format input text content when you are typing...

Pros of Cleave.js

  • More comprehensive input formatting options, including credit card, phone, date, and numeric inputs
  • Supports multiple frameworks (React, Angular, etc.) out of the box
  • Larger community and more frequent updates

Cons of Cleave.js

  • Larger file size and potentially higher performance overhead
  • More complex setup and configuration for simple use cases
  • Less flexible for custom mask patterns

Code Comparison

Maska:

import Maska from 'maska'

Maska.create('#phone', {
  mask: '+1 (###) ###-####'
})

Cleave.js:

import Cleave from 'cleave.js'

new Cleave('#phone', {
  phone: true,
  phoneRegionCode: 'US'
})

Both libraries offer straightforward ways to implement input masking, but Cleave.js provides more built-in options for specific input types, while Maska focuses on simplicity and customization through mask patterns. Cleave.js may be preferred for projects requiring diverse input formatting, while Maska could be more suitable for lightweight implementations with custom mask patterns.

Input mask for React, Angular, Ember, Vue, & plain JavaScript

Pros of text-mask

  • More comprehensive documentation and examples
  • Supports multiple frameworks (React, Angular, Vue, vanilla JS)
  • Larger community and more frequent updates

Cons of text-mask

  • Larger bundle size due to multiple framework support
  • More complex setup process for some use cases
  • Less flexible for custom mask patterns

Code Comparison

text-mask:

import { createTextMaskInputElement } from 'text-mask-core'

const maskedInputElement = createTextMaskInputElement({
  inputElement: document.querySelector('#my-input-element'),
  mask: ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]
})

maska:

import Maska from 'maska'

const mask = new Maska('#my-input-element', {
  mask: '(#00) 000-0000'
})

Key Differences

  • text-mask uses an array-based mask definition, while maska uses a string-based pattern
  • text-mask requires more setup code, but offers more granular control
  • maska provides a simpler API and more intuitive mask patterns for common use cases

Both libraries offer input masking functionality, but they cater to different needs. text-mask is more suitable for complex, multi-framework projects, while maska is ideal for simpler use cases and quicker implementation.

A jQuery Plugin to make masks on form fields and HTML elements.

Pros of jQuery-Mask-Plugin

  • Extensive browser support, including older versions
  • Large community and long-standing reputation
  • Comprehensive documentation and examples

Cons of jQuery-Mask-Plugin

  • Dependency on jQuery library
  • Larger file size due to additional features
  • Less frequent updates and maintenance

Code Comparison

jQuery-Mask-Plugin:

$('#date').mask('00/00/0000');
$('#phone').mask('(000) 000-0000');
$('#money').mask('000.000.000.000.000,00', {reverse: true});

Maska:

import Maska from 'maska'

Maska.create('#date', {mask: '##/##/####'})
Maska.create('#phone', {mask: '(###) ###-####'})
Maska.create('#money', {mask: '###.###.###.###.###,##', reversed: true})

Key Differences

  • Maska is a lightweight, modern alternative with no dependencies
  • jQuery-Mask-Plugin offers more customization options
  • Maska uses a simpler API and is easier to integrate with modern frameworks
  • jQuery-Mask-Plugin has better support for complex masking scenarios

Performance

  • Maska generally performs better due to its lightweight nature
  • jQuery-Mask-Plugin may have slower performance on older browsers

Compatibility

  • jQuery-Mask-Plugin works well with older projects using jQuery
  • Maska is more suitable for modern JavaScript applications and frameworks

VanillaMasker is a pure javascript mask input

Pros of vanilla-masker

  • Lightweight and dependency-free, making it easier to integrate into projects
  • Supports both browser and Node.js environments
  • Offers a wider range of masking patterns, including custom patterns

Cons of vanilla-masker

  • Less actively maintained, with fewer recent updates
  • Limited documentation and examples compared to Maska
  • Lacks some advanced features like dynamic masking

Code Comparison

Maska:

import Maska from 'maska'

Maska.create('#phone', {
  mask: '+1 (###) ###-####'
})

vanilla-masker:

import VMasker from 'vanilla-masker'

VMasker(document.querySelector('#phone')).maskPattern('+1 (999) 999-9999')

Both libraries offer similar functionality for basic masking, but Maska provides a more modern API and additional features. Maska uses a declarative approach with options, while vanilla-masker uses method chaining. Maska's syntax is generally more intuitive and easier to read.

Maska offers better TypeScript support and more frequent updates, making it a more robust choice for modern web development. However, vanilla-masker's simplicity and lack of dependencies may be preferable for smaller projects or those with strict size constraints.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Simple zero-dependency input mask.

Documentation & demo

Maska

Support ☕️

❤️ Please support Maska development!

Features ✨

  • No dependencies and small size: ~3 Kb gziped
  • Vanilla JS version + Vue 2/3, Alpine.js and Svelte integrations
  • Works with native and custom inputs
  • Custom tokens with modifiers, transform functions and hooks
  • Number mask mode: easy money formatting
  • Dynamic, reversed and eager masks

What’s new in v3 🎉

  • Integrations with Alpine.js and Svelte
  • Number mask mode
  • Simplified directive format
  • Other small improvements and fixes

Source of Inspiration 💡

License 📄

MIT

NPM DownloadsLast 30 Days