angular-mask

ngMask

Best Angular JS mask alternative!

Angular mask without dependencies (NO JQUERY). Pure javascript and only ~6kb! It gives you power to create your mask with optional fields in accordance with your business.

GitHub version Bower version Build Status Code Climate Test Coverage NPM Dependencies

Chrome Firefox IE8+ Opera Safari

TL;DR

Check live examples here.

Install

With Bower * Install the dependency:

  • Add ngMask.min.js to your code:

  • Include module dependency:

Whitout Bower: * Download the ngMask.min.js file from dist folder:

  • Add ngMask.min.js to your code:

  • Include module dependency:

How To Use

  1. Add ngMask plugin after your AngularJS.

  2. Add ngMask module dependency to your app.

  3. Use the avaiable patterns to create your mask. Set the mask attribute.

  4. Adjust your mask options.

Available Patterns

You can make your mask using some patterns available. If you use a pattern not specified below to construct your mask, It'll be considered a divisor. A dividor is a character used to group semantic elements, for example: "/" in dates to separate days, months and years, "-" in SSN to separate area, group and serial numbers or "." in IPv4 to create 4 groups of 8 bits.

Common Patterns

  • * =====> /./

    • Any single character
  • w =====> /\w/

    • Any word character (letter, number, underscore)
  • W =====> /\W/

    • Any non-word character
  • d =====> /\d/

    • Any digit
  • D =====> /\D/

    • Any non-digit
  • s =====> /\s/

    • Any whitespace character
  • S =====> /\S/

    • Any non-whitespace character
  • b =====> /\b/

    • Any word boundary

Number Patterns

  • 9 =====> /[0-9]/

    • Valid numbers: 0,1,2,3,4,5,6,7,8,9
  • 8 =====> /[0-8]/

    • Valid numbers: 0,1,2,3,4,5,6,7,8
  • 7 =====> /[0-7]/

    • Valid numbers: 0,1,2,3,4,5,6,7
  • 6 =====> /[0-6]/

    • Valid numbers: 0,1,2,3,4,5,6
  • 5 =====> /[0-5]/

    • Valid numbers: 0,1,2,3,4,5
  • 4 =====> /[0-4]/

    • Valid numbers: 0,1,2,3,4
  • 3 =====> /[0-3]/

    • Valid numbers: 0,1,2,3
  • 2 =====> /[0-2]/

    • Valid numbers: 0,1,2
  • 1 =====> /[0-1]/

    • Valid numbers: 0,1
  • 0 =====> /[0]/

    • Valid numbers: 0

Especial Patterns

  • ? =====> /?/

    • Optional character. It makes the previous pattern optional.
  • A =====> /[A-Z]/

    • Any uppercase alphabet letter without accents
  • a =====> /[a-z]/

    • Any lowercase alphabet letter without accents
  • Z =====> /[A-ZÇÀÁÂÃÈÉÊẼÌÍÎĨÒÓÔÕÙÚÛŨ]/

    • Any uppercase alphabet letter with accents
  • z =====> /[a-zçáàãâéèêẽíìĩîóòôõúùũüû]/

    • Any lowercase alphabet letter with accents
  • @ =====> /[a-zA-Z]/

    • Any alphabet letter without accents
  • # =====> /[a-zA-ZçáàãâéèêẽíìĩîóòôõúùũüûÇÀÁÂÃÈÉÊẼÌÍÎĨÒÓÔÕÙÚÛŨ]/

    • Any alphabet letter with accents
  • % =====> /[0-9a-zA-zçáàãâéèêẽíìĩîóòôõúùũüûÇÀÁÂÃÈÉÊẼÌÍÎĨÒÓÔÕÙÚÛŨ]/

    • Any digit and alphabet letter with accents

Options

  • NgModel (required): model object
    • Attribute 'ng-model'
    • The model of input.
  • Mask (required): mask definition
    • Attribute 'mask'
    • The mask for input.
  • NgValue: initial mask value (default: undefined)
    • Attribute 'ng-value'
    • The initial value of input.
  • Restrict: 'select', 'remove' or 'accept' (default: select)
    • Attribute 'mask-restrict' or 'restrict'
    • The way how ngMask will interact with user input.
      • Select restriction: The input will show the char inputted even on error cases. If it has errors, the wrong char will be selected.
      • Reject restriction: The input will show the char inputted on successfull cases. If it has errors, the wrong char will be rejected.
      • Accept restriction: The input will always show the char inputted. No matter it's right or wrong.
  • Repeat: number - repeat mask n times (default: undefined)
    • Attribute 'mask-repeat' or 'repeat'
    • Repeats the mask attribute the defined times.
  • Clean: 'true' or 'false' (default: false)
    • Attribute 'mask-clean' or 'clean'
    • Cleans model value. Removes divisors from model value.
  • Validate: 'true' or 'false' (default: true)
    • Attribute 'mask-validate' or 'validate'
    • Applies validation. Uses form controller setValidity method.
  • Limit: 'true' or 'false' (default: true)
    • Attribute 'mask-limit' or 'limit'
    • Limits the max length inputted according with mask.

Examples

How to ngMask

License

Open Source

The MIT License (MIT)

Copyright (c) 2014 @candreoliveira

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

comments powered by Disqus
This page was last updated almost 3 years ago.