Skip to content

Commit 1ce35bb

Browse files
committed
ie8 and non-ie8 versions
1 parent 2b4c5b6 commit 1ce35bb

File tree

4 files changed

+260
-72
lines changed

4 files changed

+260
-72
lines changed

js/input-mask.ie8.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/input-mask.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/masking-input.ie8support.js

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
var InputMask = function ( options ) {
2+
if ( options && options.masked ) {
3+
// Make it easy to wrap this plugin and pass elements instead of a selector
4+
options.masked = typeof options.masked === string ? document.querySelectorAll( options.masked ) : options.masked;
5+
}
6+
7+
if ( options ) {
8+
this.options = {
9+
masked: options.masked || document.querySelectorAll( this.defaults.masked ),
10+
maskedNumber: options.maskedNumber || this.defaults.maskedNumber,
11+
maskedLetter: options.maskedLetter || this.defaults.maskedLetter,
12+
error: options.onError || this.defaults.onError
13+
}
14+
} else {
15+
this.options = this.defaults;
16+
this.options.masked = document.querySelectorAll( this.options.masked );
17+
}
18+
19+
this.refresh( true );
20+
};
21+
22+
var inputMask = {
23+
24+
// Default Values
25+
defaults: {
26+
masked : '.masked',
27+
maskedNumber : 'XdDmMyY9',
28+
maskedLetter : '_',
29+
noValidate: '',
30+
onError: function(){}
31+
},
32+
33+
refresh: function(init) {
34+
var input, parentClass;
35+
36+
if ( !init ) {
37+
this.options.masked = document.querySelectorAll( this.options.masked );
38+
}
39+
40+
for(i = 0; i < this.options.masked.length; i++) {
41+
input = this.options.masked[i]
42+
parentClass = input.parentNode.getAttribute('class');
43+
44+
if ( !parentClass || ( parentClass && parentClass.indexOf('shell') === -1 ) ) {
45+
this.createShell(input);
46+
if ( this.options.noValidate ) {
47+
this.noValidateSetup(input);
48+
}
49+
this.activateMasking(input);
50+
}
51+
}
52+
},
53+
54+
noValidate: function(input) {
55+
this.getForm( input ).setAttribute('noValidate',true);
56+
},
57+
58+
// Remove when IE8 dies
59+
getForm: function(input) {
60+
61+
// Support: IE8 Only
62+
// IE8 does not support the form attribute and when it is supplied. It overwrites the form prop
63+
// with a string, so we need to find the proper form.
64+
return typeof input.form === "string" ? this.closestForm( input ): input.form;
65+
66+
},
67+
68+
// Remove when IE8 dies
69+
closestForm: function(input) {
70+
var parent = input.parentNode;
71+
for (;parent.parentNode; parent = parent.parentNode) {
72+
if(parent.tagName.toUpperCase() === 'form') {
73+
return parent;
74+
}
75+
}
76+
return document.body;
77+
},
78+
79+
// replaces each masked input with a shall containing the input and it's mask.
80+
createShell : function (input) {
81+
var wrap = document.createElement('span'),
82+
mask = document.createElement('span'),
83+
emphasis = document.createElement('i'),
84+
inputClass = input.getAttribute('class'),
85+
placeholderText = input.getAttribute('placeholder'),
86+
placeholder = document.createTextNode(placeholderText);
87+
88+
input.setAttribute('maxlength', placeholder.length);
89+
input.setAttribute('data-placeholder', placeholderText);
90+
input.removeAttribute('placeholder');
91+
92+
93+
if ( !inputClass || ( inputClass && inputClass.indexOf('masked') === -1 ) ) {
94+
input.setAttribute( 'class', inputClass + ' masked');
95+
}
96+
97+
mask.setAttribute('aria-hidden', 'true');
98+
mask.setAttribute('id', input.getAttribute('id') + 'Mask');
99+
mask.appendChild(emphasis);
100+
mask.appendChild(placeholder);
101+
102+
wrap.setAttribute('class', 'shell');
103+
wrap.appendChild(mask);
104+
input.parentNode.insertBefore( wrap, input );
105+
wrap.appendChild(input);
106+
},
107+
108+
setValueOfMask : function (e) {
109+
var value = e.target.value,
110+
placeholder = e.target.getAttribute('data-placeholder');
111+
112+
return "<i>" + value + "</i>" + placeholder.substr(value.length);
113+
},
114+
115+
// add event listeners
116+
activateMasking : function (input) {
117+
var that = this;
118+
if (input.addEventListener) { // remove "if" after death of IE 8
119+
input.addEventListener('keyup', function(e) {
120+
that.handleValueChange.call(that,e);
121+
}, false);
122+
} else if (input.attachEvent) { // For IE 8
123+
input.attachEvent('onkeyup', function(e) {
124+
e.target = e.srcElement;
125+
that.handleValueChange.call(that, e);
126+
});
127+
}
128+
},
129+
130+
handleValueChange : function (e) {
131+
var id = e.target.getAttribute('id');
132+
133+
if(e.target.value == document.querySelector('#' + id + 'Mask i').innerHTML) {
134+
return; // Continue only if value hasn't changed
135+
}
136+
137+
document.getElementById(id).value = this.handleCurrentValue(e);
138+
document.getElementById(id + 'Mask').innerHTML = this.setValueOfMask(e);
139+
140+
},
141+
142+
handleCurrentValue : function (e) {
143+
var isCharsetPresent = e.target.getAttribute('data-charset'),
144+
placeholder = isCharsetPresent || e.target.getAttribute('data-placeholder'),
145+
value = e.target.value, l = placeholder.length, newValue = '',
146+
i, j, isInt, isLetter, strippedValue;
147+
148+
// strip special characters
149+
strippedValue = isCharsetPresent ? value.replace(/\W/g, "") : value.replace(/\D/g, "");
150+
151+
for (i = 0, j = 0; i < l; i++) {
152+
isInt = !isNaN(parseInt(strippedValue[j]));
153+
isLetter = strippedValue[j] ? strippedValue[j].match(/[A-Z]/i) : false;
154+
matchesNumber = this.options.maskedNumber.indexOf(placeholder[i]) >= 0;
155+
matchesLetter = this.options.maskedLetter.indexOf(placeholder[i]) >= 0;
156+
if ((matchesNumber && isInt) || (isCharsetPresent && matchesLetter && isLetter)) {
157+
newValue += strippedValue[j++];
158+
} else if ((!isCharsetPresent && !isInt && matchesNumber) || (isCharsetPresent && ((matchesLetter && !isLetter) || (matchesNumber && !isInt)))) {
159+
//this.options.onError( e ); // write your own error handling function
160+
return newValue;
161+
} else {
162+
newValue += placeholder[i];
163+
}
164+
// break if no characters left and the pattern is non-special character
165+
if (strippedValue[j] == undefined) {
166+
break;
167+
}
168+
}
169+
if (e.target.getAttribute('data-valid-example')) {
170+
return this.validateProgress(e, newValue);
171+
}
172+
return newValue;
173+
},
174+
175+
validateProgress : function (e, value) {
176+
var validExample = e.target.getAttribute('data-valid-example'),
177+
pattern = new RegExp(e.target.getAttribute('pattern')),
178+
placeholder = e.target.getAttribute('data-placeholder'),
179+
l = value.length, testValue = '';
180+
181+
//convert to months
182+
if (l == 1 && placeholder.toUpperCase().substr(0,2) == 'MM') {
183+
if(value > 1 && value < 10) {
184+
value = '0' + value;
185+
}
186+
return value;
187+
}
188+
// test the value, removing the last character, until what you have is a submatch
189+
for ( i = l; i >= 0; i--) {
190+
testValue = value + validExample.substr(value.length);
191+
if (pattern.test(testValue)) {
192+
return value;
193+
} else {
194+
value = value.substr(0, value.length-1);
195+
}
196+
}
197+
198+
return value;
199+
}
200+
};
201+
202+
for ( var property in inputMask ) {
203+
if (inputMask.hasOwnProperty(property)) {
204+
InputMask.prototype[ property ] = inputMask[ property ];
205+
}
206+
}
207+
208+
// Declaritive initalization
209+
(function(){
210+
var scripts = document.getElementsByTagName('script'),
211+
script = scripts[ scripts.length - 1 ];
212+
if ( script.getAttribute('data-autoinit') ) {
213+
new InputMask();
214+
}
215+
})();
216+

0 commit comments

Comments
 (0)