diff --git a/build/vanilla-masker.min.js b/build/vanilla-masker.min.js index 80f60fa..8483a6d 100644 --- a/build/vanilla-masker.min.js +++ b/build/vanilla-masker.min.js @@ -1 +1 @@ -!function(a,b){"function"==typeof define&&define.amd?define(b):"object"==typeof exports?module.exports=b():a.VMasker=b()}(this,function(){var a=[9,16,17,18,36,37,38,39,40,91,92,93],b=function(b){for(var c=0,d=a.length;cs?b.precision:s))}return(b.unit+p+l+b.separator+m).replace(j,"")+b.suffixUnit},f.toPattern=function(a,b){var c,e="object"==typeof b?b.pattern:b,f=e.replace(/\W/g,""),g=e.split(""),h=a.toString().replace(/\W/g,""),i=h.replace(/\W/g,""),j=0,k=g.length,l="object"==typeof b?b.placeholder:void 0;for(c=0;c=h.length){if(f.length==i.length)return g.join("");if(void 0!==l&&f.length>i.length)return d(g,c,l).join("");break}if("9"===g[c]&&h[j].match(/[0-9]/)||"A"===g[c]&&h[j].match(/[a-zA-Z]/)||"S"===g[c]&&h[j].match(/[0-9a-zA-Z]/))g[c]=h[j++];else if("9"===g[c]||"A"===g[c]||"S"===g[c])return void 0!==l?d(g,c,l).join(""):g.slice(0,c).join("")}return g.join("").substr(0,c)},f.toNumber=function(a){return a.toString().replace(/(?!^-)[^0-9]/g,"")},f.toAlphaNumeric=function(a){return a.toString().replace(/[^a-z0-9 ]+/i,"")},f}); \ No newline at end of file +!function(a,b){"function"==typeof define&&define.amd?define(b):"object"==typeof exports?module.exports=b():a.VMasker=b()}(this,function(){var a=[9,16,17,18,36,37,38,39,40,91,92,93],b=function(b){for(var c=0,d=a.length;c0&&(h+="0"*j),h=h.replace(/[\D]/g,"");var k=new RegExp("^(0|\\"+b.delimiter+")"),l=new RegExp("(\\"+b.separator+")$"),m=h.substr(0,h.length-b.moneyPrecision),n=m.substr(0,m.length%3),o=new Array(b.precision+1).join("0");m=m.substr(m.length%3,m.length);for(var p=0,q=m.length;pu?b.precision:u))}return(b.unit+r+n+b.separator+o).replace(l,"")+b.suffixUnit},f.toPattern=function(a,b){var c="object"==typeof b?b.placeholder:void 0;if(!(c||a&&a.toString().replace(/\W/g,"")))return"";var e,f="object"==typeof b?b.pattern:b,g=f.replace(/\W/g,""),h=f.split(""),i=a.toString().replace(/\W/g,""),j=i.replace(/\W/g,""),k=0,l=h.length;for(e=0;e=i.length){if(g.length==j.length)return h.join("");if(void 0!==c&&g.length>j.length)return d(h,e,c).join("");break}if("9"===h[e]&&i[k].match(/[0-9]/)||"A"===h[e]&&i[k].match(/[a-zA-Z]/)||"S"===h[e]&&i[k].match(/[0-9a-zA-Z]/))h[e]=i[k++];else{if("9"===h[e]||"A"===h[e]||"S"===h[e])return void 0!==c?d(h,e,c).join(""):h.slice(0,e).join("");h[e]===i[k]&&k++}}return h.join("").substr(0,e)},f.toNumber=function(a){return a&&a.toString().replace(/\D/g,"")?a.toString().replace(/(?!^-)[^0-9]/g,""):""},f.toAlphaNumeric=function(a){return a&&a.toString().replace(/[^a-z0-9 ]+/i,"")?a.toString().replace(/[^a-z0-9 ]+/i,""):""},f}); \ No newline at end of file diff --git a/build/vanilla-masker.min.js.gz b/build/vanilla-masker.min.js.gz index cb0bf94..4a404f2 100644 Binary files a/build/vanilla-masker.min.js.gz and b/build/vanilla-masker.min.js.gz differ diff --git a/lib/vanilla-masker.js b/lib/vanilla-masker.js index b861b75..aa33526 100644 --- a/lib/vanilla-masker.js +++ b/lib/vanilla-masker.js @@ -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; @@ -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; @@ -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); @@ -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); } @@ -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++) { @@ -170,8 +179,7 @@ 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; @@ -179,6 +187,12 @@ }; 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(""), @@ -186,9 +200,7 @@ 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 @@ -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, ""); };