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.
=======
TL;DR
Install
With Bower
-
Install the dependency:
bower install angular-mask --save
-
Add ngMask.min.js to your code:
<script src='bower_components/angular-mask/dist/ngMask.min.js'></script>
-
Include module dependency:
angular.module('yourApp', ['ngMask']);
Without Bower:
-
Download the ngMask.min.js file from dist folder:
wget https://raw.githubusercontent.com/candreoliveira/ngMask/master/dist/ngMask.min.js
-
Add ngMask.min.js to your code:
<script src='ngMask.min.js'></script>
-
Include module dependency:
angular.module('yourApp', ['ngMask']);
======
How To Use
-
Add ngMask plugin after your AngularJS.
<script src="angular.min.js"></script> <script src='ngMask.min.js'></script>
-
Add ngMask module dependency to your app.
angular.module('yourApp', ['ngMask']);
-
Use the avaiable patterns to create your mask. Set the mask attribute.
<input type='text' ng-model='maskModel' mask='39/19/9999' />
- "/" isn't a pattern. It's considered a divisor. Every divisor is automatically written by ngMask. Check all available patterns here.
-
Adjust your mask options.
<input type='text' ng-model='maskModel ng-value='0/3/9' mask='3/9?' mask-repeat='2' mask-restrict='accept' mask-clean='true' mask-validate='false' mask-limit='false' />
- Generated mask '3/9?3/9?'. Check all available options here.
======
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', 'reject' 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 successful 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
======
License
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.