Skip to content

Commit

Permalink
New update that allow to use multiple masks in the same input
Browse files Browse the repository at this point in the history
  • Loading branch information
Wender Lima committed Feb 13, 2015
1 parent 07d26bb commit cfec14d
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 37 deletions.
8 changes: 8 additions & 0 deletions .idea/angular-simple-input-mask.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/scopes/scope_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,6 +13,13 @@ Directive will look for attribute angular-mask on input
<input name="test" maxlengh="19" angular-mask="0000-0000-0000-0000" />
```

### Multiple masks
```HTML
<input name="test" maxlengh="19" angular-mask="(00)00000-0000|(00)0000-0000" />
```
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
bower install angular-input-mask --save
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
72 changes: 40 additions & 32 deletions src/angular-simple-mask.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
};
});
};
});

0 comments on commit cfec14d

Please sign in to comment.