Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/vanilla-masker.min.js

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

Binary file modified build/vanilla-masker.min.js.gz
Binary file not shown.
43 changes: 30 additions & 13 deletions lib/vanilla-masker.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@

if (isAllowedKeyCode(e.keyCode)) {
setTimeout(function() {
var rawValue = source.value.replace(/\D/g, '');

if (!rawValue) {
source.value = '';
source.lastOutput = '';
return;
}

that.opts.lastOutput = source.lastOutput;
source.value = VMasker[maskFunction](source.value, that.opts);
source.lastOutput = source.value;
Expand All @@ -77,8 +85,7 @@
}
}, 0);
}
}
;
};
for (var i = 0, len = this.elements.length; i < len; i++) {
this.elements[i].lastOutput = "";
this.elements[i].onkeyup = onType;
Expand Down Expand Up @@ -121,14 +128,17 @@
};

VMasker.toMoney = function(value, opts) {
if (value === undefined || value === null || value === '') {
return '';
}

opts = mergeMoneyOptions(opts);
if (opts.zeroCents) {
opts.lastOutput = opts.lastOutput || "";
var zeroMatcher = ("("+ opts.separator +"[0]{0,"+ opts.precision +"})"),
zeroRegExp = new RegExp(zeroMatcher, "g"),
digitsLength = value.toString().replace(/[\D]/g, "").length || 0,
lastDigitLength = opts.lastOutput.toString().replace(/[\D]/g, "").length || 0
;
lastDigitLength = opts.lastOutput.toString().replace(/[\D]/g, "").length || 0;
value = value.toString().replace(zeroRegExp, "");
if (digitsLength < lastDigitLength) {
value = value.slice(0, value.length - 1);
Expand All @@ -140,7 +150,7 @@
var separatorIndex = number.indexOf(opts.separator),
missingZeros = (opts.precision - (number.length - separatorIndex - 1));

if (separatorIndex !== -1 && (missingZeros > 0) ) {
if (separatorIndex !== -1 && (missingZeros > 0)) {
number = number + ('0' * missingZeros);
}

Expand All @@ -150,8 +160,7 @@
clearSeparator = new RegExp("(\\"+ opts.separator +")$"),
money = number.substr(0, number.length - opts.moneyPrecision),
masked = money.substr(0, money.length % 3),
cents = new Array(opts.precision + 1).join("0")
;
cents = new Array(opts.precision + 1).join("0");

money = money.substr(money.length % 3, money.length);
for (var i = 0, len = money.length; i < len; i++) {
Expand All @@ -170,25 +179,28 @@
var beginCents = Math.max(0, number.length - opts.precision),
centsValue = number.substr(beginCents, opts.precision),
centsLength = centsValue.length,
centsSliced = (opts.precision > centsLength) ? opts.precision : centsLength
;
centsSliced = (opts.precision > centsLength) ? opts.precision : centsLength;
cents = (cents + centsValue).slice(-centsSliced);
}
var output = opts.unit + signal + masked + opts.separator + cents;
return output.replace(clearSeparator, "") + opts.suffixUnit;
};

VMasker.toPattern = function(value, opts) {
var placeholder = (typeof opts === 'object' ? opts.placeholder : undefined);

if (!placeholder && (!value || !value.toString().replace(/\W/g, ''))) {
return '';
}

var pattern = (typeof opts === 'object' ? opts.pattern : opts),
patternChars = pattern.replace(/\W/g, ''),
output = pattern.split(""),
values = value.toString().replace(/\W/g, ""),
charsValues = values.replace(/\W/g, ''),
index = 0,
i,
outputLength = output.length,
placeholder = (typeof opts === 'object' ? opts.placeholder : undefined)
;
outputLength = output.length;

for (i = 0; i < outputLength; i++) {
// Reached the end of input
Expand Down Expand Up @@ -220,17 +232,22 @@
} else if (output[i] === values[index]) {
index++;
}

}
}
return output.join("").substr(0, i);
};

VMasker.toNumber = function(value) {
if (!value || !value.toString().replace(/\D/g, '')) {
return '';
}
return value.toString().replace(/(?!^-)[^0-9]/g, "");
};

VMasker.toAlphaNumeric = function(value) {
if (!value || !value.toString().replace(/[^a-z0-9 ]+/i, '')) {
return '';
}
return value.toString().replace(/[^a-z0-9 ]+/i, "");
};

Expand Down