• Stars
    star
    485
  • Rank 90,698 (Top 2 %)
  • Language
    HTML
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Framework agnostic accessible input masking library

Input Masking

Features

input-mask.js enables you to include a mask on any input where a specific data entry format is required. The placeholder text remains in place, displaying which characters still need to be included. The placeholder is CSS styleable.

The user can enter letters and numbers. All other characters, like spaces, dashes, and parenthesis are automatically added by the script, making data entery easier when using dynamic keypads.

Example

http://estelle.github.io/input-masking

Quick Start

Include masking-input.js.

<script src="path/js/masking-input.js" data-autoinit="true"></script>`

Add either the css file masking-input.css to your page, or incorporate the .scss component into your sass build

<link rel="stylesheet" href="path/css/masking-input.css"/>

Add inputs with id, placeholder and pattern attributes with the class of masked. Include type="tel" when requiring numbers only.

	 <label for="zip">Zip Code</label>
  	 <input id="zip" type="tel" placeholder="XXXXX" pattern="\d{5}" 
  	     class="masked" name="uszipcode" title="5-digit zip code"> 

If your placeholder includes non-digits and non-letters, no worries. They'll be added by the script. Your mobile users won't have to change their touch keyboards. They simply need to enter letters.

Do make sure that your placeholder would match the regular expression of your pattern if all the X's were converted to integers.

If your regular expressions include letters, you must include the made up data-charset attribute. Similar to the pattern, include an X for each number and an underscore _ for each required letter.

	<label for="zipca">Canadian Zip Code</label>
  	<input placeholder="XXX XXX" pattern="\w\d\w \d\w\d" class="masked" 
  		data-charset="_X_ X_X" id="zipca" type="text" name="zipcodeca" 
  	    title="6-character alphanumeric zip code in the format of A1A 1A1">

If the digits allowed by your regular expression are constrained or complicated, such as months only allowing 01-12, include a made up data-valid-example attribute that takes as its value a valid value that would match the pattern.

	<label for="expiration"> Credit Card Expiration </label>
    <input id="expiration" type="tel" placeholder="MM/YY" class="masked" 
    	pattern="(1[0-2]|0[1-9])\/(1[5-9]|2\d)" 
    	data-valid-example="05/18"> 

Accessibility

There are accessibility features baked into the examples that you must maintain to maintain accessibility.

  • Always include a label for each form control, and associate the form control either implicitly by nesting it, or explicitly with the for and id attributes.
  • Always include a title attribute that describes the expected pattern when including the pattern attribute.
  • Always use the best input type for the job, so dynamic touchpad users get the right touchpad for the job. Generally this will always be type="tel", as most masking is for digits only. However, when alphanumeric characters are required, use type="text". And, while I've included an expiration month to show an example of using complex regular expressions, use type="month" instead of this script.

Browser Support

  • Safari
  • Chrome
  • Firefox
  • Opera
  • IE 8

Customization

Initalization

If you are ok with all the default options you can have masked-inputs initalize it self and avoid writing any JavaScript at all. Simply add the attribute data-autoinit="false" to your script tag

<script src="path/js/masking-input.js" data-autoinit="true"></script>

Alternativly if you need to pass custom options or want to initalize the script your self you can do so like this

new InputMask( */options*/ );

Options

masked

Don't like the class masked for a selector? Pass an options object instead and set your selector with the masked property or event pass a nodeList.

// Selector
new InputMask({
  masked: ".custom-selector"
});

// Node List
new InputMask({
  masked: nodeList
});

number

Want to use something other than X? right now the script handles XdDmMyY9 for numeric placeholding. MM/YY and mm/yy will work fine. Want to use something else, simply pass an options object setting the number option.

// Selector
new InputMask({
  number: 'XZdDmMyY9'
});

letter

Want to use something other than X in your placeholder look for masked inputs that require both letters and numbers, you can. You can put different characters in your placeholder, as long as your data-charset contains Xs and _ only.

<input placeholder="XXX XXX" pattern="\w\d\w \d\w\d" class="masked" 
      data-charset="?X? X?X" id="zipca" type="text" name="canadianzip" 
        title="6-character alphanumeric code in the format of A1A 1A1">

If you require _ as a special character in your mask? Simply pass an options object setting the letter option, and also the value of data-charset in your HTML.

new InputMask({
  letter: '?'
});
<input placeholder="XXX_XXX" pattern="\w\d\w\_\d\w\d" class="masked" 
      data-charset="?X?_X?X" id="underscore" type="text" name="underscoredstring" 
        title="6-character alphanumeric code in the format of A1A_1A1">

onError

Want to add error handling? Simply pass a function to the onError option. The function will recieve the keyboard event as the first paramater.

new InputMask({
  onError: function( e ) {
    // Handle your errors!
  }
});

noValidate

As the pattern attribute is being used, you may want to add via javascript the novalidate attribute on any ancestor form or form control to disable native browser validation. Do add it via JS, because if the JS fails, native validation is a good alternative.

You can have input masking do this for you by setting the noValidate option to true.

new InputMask({
  noValidate: true
});

Documentation

Handles these test cases:

  • OK if the pattern starts with a special character
  • OK if the next letter is a special character
  • Can handle more than one special character
  • Doesn't matter if browser supports placeholder attribute: appears even in IE8
  • Doesn't matter if browser supports pattern attribute: still works
  • characters can be deleted or added mid input
  • Arrow keys can be used
  • Sets up maxlength based on placeholder length
  • Only uppercase letters are shown (this can be changed)
  • If user enters an invalid character, character deleted
  • Enters special characters automagically
  • No unwanted characters are read by screen reader
  • Supports keyboard, mouse and touchpad
  • Works if the user leaves the input and comes back
  • If user gives focus before a special character, jumps forward if typed in.
  • Matches simple regular expressions
  • Can be made to match complex regular expression

Exceptions

Complex Regular Expressions

If the digits allowed by your regular expression are constrained or complicated, such as months only allowing 01-12, include a made up data-valid-example attribute that takes as its value a valid value that would match the pattern.

	<label for="expiration"> Credit Card Expiration </label>
    <input id="expiration" type="tel" placeholder="MM/YY" class="masked" 
    	pattern="(1[0-2]|0[1-9])\/(1[5-9]|2\d)" 
    	data-valid-example="11/18"
    	title="2-digit month and 2-digit year greater than 01/15"> 

I've taken care of MM in masking.validateProgress(), because that is common. If you have exceptions, add the exceptions there. If you need an expiration month, it is best to use <input type="month"> instead.

ReactJS version

	var React = require('react'),
		MaskedInput = require('../index');

	window.onload = function () {
    	React.render(
	 <ul>
           <li>
	    <label htmlFor="month">Month</label>
            <MaskedInput 
            id="month" 
            type="tel" 
            placeholder="MM/YY"
            pattern="(1[0-2]|0[1-9])\/\d\d" 
            data-valid-example="11/18"
            title="2-digit month and 2-digit year greater than 01/15" />
        </li>
        <li>
          <label htmlFor="zip">Zip Code</label>
          <MaskedInput 
            id="zip" 
            type="tel" 
            placeholder="XXXXX" 
            pattern="\d{5}" 
            required
            title="5-digit zip code" />
        </li>      
        </ul>,
        document.getElementById('component')
    );
  };

Contributors

Estelle Weyl. Alex Schmitz.

License

This code is available under the MIT license

Thanks

Thanks to @stevefaulkner

More Repositories

1

clowncar

Clown Car Responsive Image Technique
HTML
876
star
2

CSS-Workshop

6 hours workshop covering almost everything in CSS2 and CSS3
HTML
115
star
3

html_elements

Template using all the HTML elements
HTML
64
star
4

cssmastery

Front End Masters CSS Mastery Class
HTML
59
star
5

selectors

Select This! CSS Selectors Presentation
HTML
36
star
6

estelle.github.com

Estelle Weyl
CSS
35
star
7

mobileperf

Mobile Performance Workshop
HTML
28
star
8

merry-go-round

ceci n'est pas un carousel
HTML
18
star
9

doyouknowcss

A few things you likely didn't know about CSS
HTML
18
star
10

jsintro

Introduction to Javascript
JavaScript
15
star
11

slides

HTML5 Slide Template
HTML
13
star
12

CSS

CSS workshop 2019
HTML
9
star
13

snow

Snow: CSS3 Animations, transitions, transforms, gradients, etc.
CSS
8
star
14

html5training

HTML5 Training with CSS
JavaScript
8
star
15

CSS-JS-Entity-Calculator

Calculator for converting characters for use in html, css and javascript
7
star
16

mobile

mobile UI Performance
JavaScript
6
star
17

flexbox

Flexbox tutorial
HTML
6
star
18

animation

CSS Transitions and Animations
HTML
5
star
19

StopSOPA

makes your page disappear on January 18, 2012
JavaScript
5
star
20

iphone

native iPhone app look and feel with CSS only
4
star
21

forms

HTML5 Web Forms Presentation
HTML
4
star
22

carousel-carousel

Script for creating simple, accessible carousels
HTML
3
star
23

simplyaccessible

"Simply Accessible Web Performance" Presentation
HTML
3
star
24

mobilecss

CSS Workshop focusing on what you need for mobile
JavaScript
3
star
25

oscon2012

OSCON 2012 Files
JavaScript
3
star
26

CSS3Gradients

Library of CSS3 Gradients
3
star
27

css-questions

CSS Interview Questions
3
star
28

accessu

Talk for accessU 2019
HTML
2
star
29

viewsource

Mobile Web Performance: Slide deck for ViewSource Conference London 2017
HTML
2
star
30

browsers

Web Performance: How the browser gets populate (in French)
HTML
2
star
31

csstdg

2
star
32

HSLA-Color-Converter

HSL/ HSLA / RGB / RGBA / Hex color converter
HTML
2
star
33

10

10 Things You Didn't Know Browsers Could Do
JavaScript
2
star
34

jsframeworks

When to use and not use a JS framework and why
JavaScript
2
star
35

html5mobile

Mobile Training: HTML5, CSS3 & JS APIS
JavaScript
2
star
36

estelle

1
star
37

flashback

HTML
1
star
38

animations

Animations and Transitions in CSS3: Support Files
HTML
1
star
39

a11yperf

Accessibility as Performance, but focused more on a11y.
HTML
1
star
40

begin-svelte-app

Begin app
JavaScript
1
star
41

wtf

CSS: Wonderfully Terrific Features and Weirdly Terrible Fauxpas.
HTML
1
star
42

components

Web Components
CSS
1
star
43

rendering

English Translation of Browsers: populating the page
HTML
1
star
44

cssintro

Cascading Style Sheets: Design Tools to Style Your Web Content
CSS
1
star
45

javascript-questions

JavaScript Interview Questions
1
star
46

html5jsapis

Overview of some basic CSS3 and HTML5 JS APIs
CSS
1
star
47

codelab

CSS Presentation for GTUG Codelab
1
star
48

cssdeepdive

Deep Dive into CSS Selectors, Animation and Flexbox
CSS
1
star