Skip to content

tpmanc/js-mask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Js mask

Written in vanilla JS library to add phone mask to your input elements.

Demo

Phone mask demo

Requirements

  • none

Library is written in vanilla JS and has no dependancies.

Getting Started

Download

Bower

Install via bower: bower install --save js-mask

How to use

<html>
   <head></head>
   <body>
      <input type="text" id="phone">
      <!-- Loading the library -->
      <script src="js-mask.min.js"></script>
   </body>
</html>

Add mask to input:

var inputs = new PhoneMask(document.querySelectorAll('#phone'));

To customize functionality pass settings as second parameter:

var inputs = new PhoneMask(document.querySelectorAll('#phone'), {
  pattern: '(xx) xxxx-xx-xx',
  prefix: '+7 ',
  patternChar: 'x'
});

To destroy phone mask:

var inputs = new PhoneMask(document.querySelectorAll('#phone'));
// destroy mask
inputs.destroy();

Settings

Setting Type Default Value Description
pattern String '(___) ___-__-__' Input formatting pattern
patternChar String '_' Characters in pattent to replace by entered characters
prefix String '' Phone number prefix
igrogeKeyCodes Array [9, 16, 17, 18, 36, 37, 38, 39, 40, 91, 92, 93] Array of key codes to ignore
allowedRegExp Pattern or false /^\d$/ RegExp pattent for entered characters