From cfec14de72273375481c0a600a840ba92598878b Mon Sep 17 00:00:00 2001 From: Wender Lima Date: Fri, 13 Feb 2015 11:42:29 -0200 Subject: [PATCH] New update that allow to use multiple masks in the same input --- .idea/angular-simple-input-mask.iml | 8 ++++ .idea/encodings.xml | 4 ++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 ++++ .idea/scopes/scope_settings.xml | 5 ++ .idea/vcs.xml | 6 +++ README.md | 13 ++++-- bower.json | 4 +- src/angular-simple-mask.js | 72 ++++++++++++++++------------- 9 files changed, 87 insertions(+), 37 deletions(-) create mode 100644 .idea/angular-simple-input-mask.iml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/scopes/scope_settings.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/angular-simple-input-mask.iml b/.idea/angular-simple-input-mask.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/angular-simple-input-mask.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..d821048 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8662aa9 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e2d6f75 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index d703d30..3ca2a5b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ *** -Way simple Angular directive to apply mask to input fields +Way simple Angular directive to apply mask to input fields also with dynamic/multiple masks to the same field, which means that you can have different masks for different lengths of numbers Mask format uses 0 Example: 0000-0000-0000-0000 @@ -13,6 +13,13 @@ Directive will look for attribute angular-mask on input ``` +### Multiple masks +```HTML + +``` +Use | to separate masks + + +#### Install via bower -## Demo -Do you want to see this directive in action? Visit http://wender.com.br/angular/angular-simple-mask.html \ No newline at end of file + bower install angular-input-mask --save \ No newline at end of file diff --git a/bower.json b/bower.json index 9b84444..9c906f5 100644 --- a/bower.json +++ b/bower.json @@ -1,8 +1,8 @@ { "name": "angular-simple-input-mask", - "version": "1.0.0", + "version": "1.0.2", "main": "./src/angular-simple-mask.js", "dependencies": { - "angular": "1.0.8" + "angular": "^1.0.8" } } diff --git a/src/angular-simple-mask.js b/src/angular-simple-mask.js index 9541120..17d7c1d 100644 --- a/src/angular-simple-mask.js +++ b/src/angular-simple-mask.js @@ -1,40 +1,48 @@ 'use strict'; angular.module('angularMask', []) - .directive('angularMask', function() { - return { - restrict : 'A', - link: function($scope, el, attrs) { - var format = attrs.angularMask; + .directive('angularMask', function() { + return { + restrict : 'A', + link: function($scope, el, attrs) { + var format = attrs.angularMask, + arrFormat = format.split('|'); - function mask(el, format) { - var text = el; - var value = text.value; - var newValue = ""; - for (var vId = 0, mId = 0 ; mId < format.length ; ) { - if (mId >= value.length){ - break; + if(arrFormat.length > 1){ + arrFormat.sort(function(a, b){ + return a.length - b.length; + }); } - if (format[mId] == '0' && value[vId].match(/[0-9]/) === null) { - break; - } - while (format[mId].match(/[0\*]/) === null) { - if (value[vId] == format[mId]){ - break; + function mask(o) { + var value = o.value.replace(/\D/g,''); + if(arrFormat.length > 1){ + for(var a in arrFormat){ + if(value.replace(/\D/g,'').length <= arrFormat[a].replace(/\D/g,'').length){ + format = arrFormat[a]; + break; + } + } + } + var newValue = ''; + for(var nmI = 0, mI = 0; mI < format.length;){ + if(format[mI].match(/\D/)){ + newValue+=format[mI]; + }else{ + if(value[nmI] != undefined){ + newValue+=value[nmI]; + nmI++; + }else{ + break; + } + } + mI++; } - newValue += format[mId++]; + o.value = newValue; } - newValue += value[vId++]; - mId++; + el.bind('keyup keydown', function(e) { + var keyList = [8,37,39,46]; + if(keyList.indexOf(e.keyCode) == -1)mask(this); + }); } - text.value = newValue; - } - - el.bind('keyup keydown', function(e) { - var _format = format; - var _el = el[0]; - mask(_el,_format); - }); - } - }; -}); \ No newline at end of file + }; + }); \ No newline at end of file