diff --git a/dist/jquery.maskMoney.js b/dist/jquery.maskMoney.js index c594a6c..a4bef55 100644 --- a/dist/jquery.maskMoney.js +++ b/dist/jquery.maskMoney.js @@ -1,3 +1,11 @@ +/* + * jquery-maskmoney - v3.0.2 + * jQuery plugin to mask data entry in the input text in the form of money (currency) + * https://github.com/plentz/jquery-maskmoney + * + * Made by Diego Plentz + * Under MIT License (https://raw.github.com/plentz/jquery-maskmoney/master/LICENSE) + */ /* * jquery-maskmoney - v3.0.2 * jQuery plugin to mask data entry in the input text in the form of money (currency) @@ -8,12 +16,71 @@ */ (function ($) { "use strict"; + if (!$.browser) { $.browser = {}; - $.browser.mozilla = /mozilla/.test(navigator.userAgent.toLowerCase()) && !/webkit/.test(navigator.userAgent.toLowerCase()); - $.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase()); - $.browser.opera = /opera/.test(navigator.userAgent.toLowerCase()); - $.browser.msie = /msie/.test(navigator.userAgent.toLowerCase()); + var browserMatched = uaMatchBrowser(navigator.userAgent); + + if (browserMatched.browser) { + $.browser[browserMatched.browser] = true; + $.browser.version = browserMatched.version; + } + + // Chrome is Webkit, but Webkit is also Safari. + if ($.browser.chrome) { + $.browser.webkit = true; + } else if ($.browser.webkit) { + $.browser.safari = true; + } + } + + if (!$.platform) { + $.platform = {}; + var platformMatched = uaMatchPlatform(navigator.userAgent); + + if(platformMatched.platform) { + $.platform[platformMatched.platform] = true; + } + + if(platformMatched.mobile) { + $.platform.mobile = true; + $.platform[platformMatched.mobile] = true; + } + } + + // jQuery Migration library code + function uaMatchBrowser(ua) { + ua = ua.toLowerCase(); + + var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) || + /(webkit)[ \/]([\w.]+)/.exec( ua ) || + /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) || + /(msie) ([\w.]+)/.exec( ua ) || + ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) || + []; + + return { + browser: match[ 1 ] || "", + version: match[ 2 ] || "0" + }; + } + + function uaMatchPlatform(ua) { + ua = ua.toLowerCase(); + + var platformMatch = /(mac)/.exec(ua) || + /(linux)/.exec(ua) || + /(windows)/.exec(ua) || + []; + + var mobileMatch = /(android)/.exec(ua) || + /(iphone)/.exec(ua) || + []; + + return { + platform: platformMatch[1] || "", + mobile: mobileMatch[1] || "" + }; } var methods = { @@ -166,6 +233,43 @@ return operator + settings.prefix + value + settings.suffix; } + function formatValue(value) { + var negative = (value.indexOf("-") > -1 && settings.allowNegative) ? "-" : "", + decimalIndex = value.indexOf(settings.decimal), + integerPart = (decimalIndex === -1 ? value : value.slice(0, decimalIndex)).replace(/\D/g, ""), + decimalPart = (decimalIndex === -1 ? "" : value.slice(decimalIndex+1)).replace(/\D/g, ""), + newValue; + + // remove initial zeros + integerPart = integerPart.replace(/^0+/g, ""); + + if (integerPart === "") { + integerPart = "0"; + } + + newValue = negative + integerPart; + + if ((decimalIndex !== -1) && (settings.precision > 0)) { + var decimalRemoved = ""; + + if(decimalPart.length > settings.precision) { + decimalRemoved = parseFloat(decimalPart.substring(settings.precision)); + decimalPart = decimalPart.substring(0, settings.precision); + } + + newValue += settings.decimal + decimalPart; + + if((parseFloat(newValue, 10) === 0) && (decimalRemoved > 0)) { + // Format the current number typed by the user as an integer part + // when cursor positioned on the right side and the current value + // is '0.00', considereing two decimal places (0.005 > 5). + return formatValue(decimalRemoved.toString()); + } + } + + return setSymbol(newValue); + } + function maskValue(value) { var negative = (value.indexOf("-") > -1 && settings.allowNegative) ? "-" : "", onlyNumbers = value.replace(/[^0-9]/g, ""), @@ -176,6 +280,7 @@ // remove initial zeros integerPart = integerPart.replace(/^0*/g, ""); + // put settings.thousands every 3 chars integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, settings.thousands); if (integerPart === "") { @@ -237,6 +342,9 @@ //added to handle an IE "special" event if (key === undefined) { return false; + } else if(key === 0) { + // Chrome Mobile doesn't provides the key code + return true; } // any key except the numbers 0-9 @@ -326,6 +434,26 @@ } } + // It's just a workaround for Android devices because Chrome + // mobile doesn't support `keypress` event either provides the + // key code on `keydown`, `keyup` or `compositionupdate` events + function keyupEvent() { + var value = $input.val(); + var formattedValue = formatValue(value); + + if(value !== formattedValue) { + $input.val(formattedValue); + } + } + + function focusAndSelect() { + focusEvent(); + setTimeout(function() { + $input.select(); + }, 50); + + } + function focusEvent() { onFocusValue = $input.val(); mask(); @@ -349,6 +477,11 @@ return (n.toFixed(settings.precision)).replace(new RegExp("\\.", "g"), settings.decimal); } + function maskAndBlur(e) { + mask(); + blurEvent(e); + } + function blurEvent(e) { if ($.browser.msie) { keypressEvent(e); @@ -385,11 +518,19 @@ } $input.unbind(".maskMoney"); - $input.bind("keypress.maskMoney", keypressEvent); - $input.bind("keydown.maskMoney", keydownEvent); - $input.bind("blur.maskMoney", blurEvent); - $input.bind("focus.maskMoney", focusEvent); - $input.bind("click.maskMoney", clickEvent); + + if($.browser.chrome && $.platform.android) { + $input.bind("keyup.maskMoney", keyupEvent); + $input.bind("blur.maskMoney", maskAndBlur); + $input.bind("focus.maskMoney", focusAndSelect); + } else { + $input.bind("keypress.maskMoney", keypressEvent); + $input.bind("keydown.maskMoney", keydownEvent); + $input.bind("blur.maskMoney", blurEvent); + $input.bind("focus.maskMoney", focusEvent); + $input.bind("click.maskMoney", clickEvent); + } + $input.bind("cut.maskMoney", cutPasteEvent); $input.bind("paste.maskMoney", cutPasteEvent); $input.bind("mask.maskMoney", mask); diff --git a/dist/jquery.maskMoney.min.js b/dist/jquery.maskMoney.min.js index b6c66ce..99cb80f 100644 --- a/dist/jquery.maskMoney.min.js +++ b/dist/jquery.maskMoney.min.js @@ -6,4 +6,4 @@ * Made by Diego Plentz * Under MIT License (https://raw.github.com/plentz/jquery-maskmoney/master/LICENSE) */ -!function($){"use strict";$.browser||($.browser={},$.browser.mozilla=/mozilla/.test(navigator.userAgent.toLowerCase())&&!/webkit/.test(navigator.userAgent.toLowerCase()),$.browser.webkit=/webkit/.test(navigator.userAgent.toLowerCase()),$.browser.opera=/opera/.test(navigator.userAgent.toLowerCase()),$.browser.msie=/msie/.test(navigator.userAgent.toLowerCase()));var a={destroy:function(){return $(this).unbind(".maskMoney"),$.browser.msie&&(this.onpaste=null),this},mask:function(a){return this.each(function(){var b,c=$(this);return"number"==typeof a&&(c.trigger("mask"),b=$(c.val().split(/\D/)).last()[0].length,a=a.toFixed(b),c.val(a)),c.trigger("mask")})},unmasked:function(){return this.map(function(){var a,b=$(this).val()||"0",c=-1!==b.indexOf("-");return $(b.split(/\D/).reverse()).each(function(b,c){return c?(a=c,!1):void 0}),b=b.replace(/\D/g,""),b=b.replace(new RegExp(a+"$"),"."+a),c&&(b="-"+b),parseFloat(b)})},init:function(a){return a=$.extend({prefix:"",suffix:"",affixesStay:!0,thousands:",",decimal:".",precision:2,allowZero:!1,allowNegative:!1},a),this.each(function(){function b(){var a,b,c,d,e,f=s.get(0),g=0,h=0;return"number"==typeof f.selectionStart&&"number"==typeof f.selectionEnd?(g=f.selectionStart,h=f.selectionEnd):(b=document.selection.createRange(),b&&b.parentElement()===f&&(d=f.value.length,a=f.value.replace(/\r\n/g,"\n"),c=f.createTextRange(),c.moveToBookmark(b.getBookmark()),e=f.createTextRange(),e.collapse(!1),c.compareEndPoints("StartToEnd",e)>-1?g=h=d:(g=-c.moveStart("character",-d),g+=a.slice(0,g).split("\n").length-1,c.compareEndPoints("EndToEnd",e)>-1?h=d:(h=-c.moveEnd("character",-d),h+=a.slice(0,h).split("\n").length-1)))),{start:g,end:h}}function c(){var a=!(s.val().length>=s.attr("maxlength")&&s.attr("maxlength")>=0),c=b(),d=c.start,e=c.end,f=c.start!==c.end&&s.val().substring(d,e).match(/\d/)?!0:!1,g="0"===s.val().substring(0,1);return a||f||g}function d(a){s.each(function(b,c){if(c.setSelectionRange)c.focus(),c.setSelectionRange(a,a);else if(c.createTextRange){var d=c.createTextRange();d.collapse(!0),d.moveEnd("character",a),d.moveStart("character",a),d.select()}})}function e(b){var c="";return b.indexOf("-")>-1&&(b=b.replace("-",""),c="-"),c+a.prefix+b+a.suffix}function f(b){var c,d,f,g=b.indexOf("-")>-1&&a.allowNegative?"-":"",h=b.replace(/[^0-9]/g,""),i=h.slice(0,h.length-a.precision);return i=i.replace(/^0*/g,""),i=i.replace(/\B(?=(\d{3})+(?!\d))/g,a.thousands),""===i&&(i="0"),c=g+i,a.precision>0&&(d=h.slice(h.length-a.precision),f=new Array(a.precision+1-d.length).join(0),c+=a.decimal+f+d),e(c)}function g(a){var b,c=s.val().length;s.val(f(s.val())),b=s.val().length,a-=c-b,d(a)}function h(){var a=s.val();s.val(f(a))}function i(){var b=s.val();return a.allowNegative?""!==b&&"-"===b.charAt(0)?b.replace("-",""):"-"+b:b}function j(a){a.preventDefault?a.preventDefault():a.returnValue=!1}function k(a){a=a||window.event;var d,e,f,h,k,l=a.which||a.charCode||a.keyCode;return void 0===l?!1:48>l||l>57?45===l?(s.val(i()),!1):43===l?(s.val(s.val().replace("-","")),!1):13===l||9===l?!0:!$.browser.mozilla||37!==l&&39!==l||0!==a.charCode?(j(a),!0):!0:c()?(j(a),d=String.fromCharCode(l),e=b(),f=e.start,h=e.end,k=s.val(),s.val(k.substring(0,f)+d+k.substring(h,k.length)),g(f+1),!1):!1}function l(c){c=c||window.event;var d,e,f,h,i,k=c.which||c.charCode||c.keyCode;return void 0===k?!1:(d=b(),e=d.start,f=d.end,8===k||46===k||63272===k?(j(c),h=s.val(),e===f&&(8===k?""===a.suffix?e-=1:(i=h.split("").reverse().join("").search(/\d/),e=h.length-i-1,f=e+1):f+=1),s.val(h.substring(0,e)+h.substring(f,h.length)),g(e),!1):9===k?!0:!0)}function m(){r=s.val(),h();var a,b=s.get(0);b.createTextRange&&(a=b.createTextRange(),a.collapse(!1),a.select())}function n(){setTimeout(function(){h()},0)}function o(){var b=parseFloat("0")/Math.pow(10,a.precision);return b.toFixed(a.precision).replace(new RegExp("\\.","g"),a.decimal)}function p(b){if($.browser.msie&&k(b),""===s.val()||s.val()===e(o()))a.allowZero?a.affixesStay?s.val(e(o())):s.val(o()):s.val("");else if(!a.affixesStay){var c=s.val().replace(a.prefix,"").replace(a.suffix,"");s.val(c)}s.val()!==r&&s.change()}function q(){var a,b=s.get(0);b.setSelectionRange?(a=s.val().length,b.setSelectionRange(a,a)):s.val(s.val())}var r,s=$(this);a=$.extend(a,s.data()),s.unbind(".maskMoney"),s.bind("keypress.maskMoney",k),s.bind("keydown.maskMoney",l),s.bind("blur.maskMoney",p),s.bind("focus.maskMoney",m),s.bind("click.maskMoney",q),s.bind("cut.maskMoney",n),s.bind("paste.maskMoney",n),s.bind("mask.maskMoney",h)})}};$.fn.maskMoney=function(b){return a[b]?a[b].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof b&&b?($.error("Method "+b+" does not exist on jQuery.maskMoney"),void 0):a.init.apply(this,arguments)}}(window.jQuery||window.Zepto); \ No newline at end of file +!function($){"use strict";function a(a){a=a.toLowerCase();var b=/(chrome)[ \/]([\w.]+)/.exec(a)||/(webkit)[ \/]([\w.]+)/.exec(a)||/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(a)||/(msie) ([\w.]+)/.exec(a)||a.indexOf("compatible")<0&&/(mozilla)(?:.*? rv:([\w.]+)|)/.exec(a)||[];return{browser:b[1]||"",version:b[2]||"0"}}function b(a){a=a.toLowerCase();var b=/(mac)/.exec(a)||/(linux)/.exec(a)||/(windows)/.exec(a)||[],c=/(android)/.exec(a)||/(iphone)/.exec(a)||[];return{platform:b[1]||"",mobile:c[1]||""}}if(!$.browser){$.browser={};var c=a(navigator.userAgent);c.browser&&($.browser[c.browser]=!0,$.browser.version=c.version),$.browser.chrome?$.browser.webkit=!0:$.browser.webkit&&($.browser.safari=!0)}if(!$.platform){$.platform={};var d=b(navigator.userAgent);d.platform&&($.platform[d.platform]=!0),d.mobile&&($.platform.mobile=!0,$.platform[d.mobile]=!0)}var e={destroy:function(){return $(this).unbind(".maskMoney"),$.browser.msie&&(this.onpaste=null),this},mask:function(a){return this.each(function(){var b,c=$(this);return"number"==typeof a&&(c.trigger("mask"),b=$(c.val().split(/\D/)).last()[0].length,a=a.toFixed(b),c.val(a)),c.trigger("mask")})},unmasked:function(){return this.map(function(){var a,b=$(this).val()||"0",c=-1!==b.indexOf("-");return $(b.split(/\D/).reverse()).each(function(b,c){return c?(a=c,!1):void 0}),b=b.replace(/\D/g,""),b=b.replace(new RegExp(a+"$"),"."+a),c&&(b="-"+b),parseFloat(b)})},init:function(a){return a=$.extend({prefix:"",suffix:"",affixesStay:!0,thousands:",",decimal:".",precision:2,allowZero:!1,allowNegative:!1},a),this.each(function(){function b(){var a,b,c,d,e,f=w.get(0),g=0,h=0;return"number"==typeof f.selectionStart&&"number"==typeof f.selectionEnd?(g=f.selectionStart,h=f.selectionEnd):(b=document.selection.createRange(),b&&b.parentElement()===f&&(d=f.value.length,a=f.value.replace(/\r\n/g,"\n"),c=f.createTextRange(),c.moveToBookmark(b.getBookmark()),e=f.createTextRange(),e.collapse(!1),c.compareEndPoints("StartToEnd",e)>-1?g=h=d:(g=-c.moveStart("character",-d),g+=a.slice(0,g).split("\n").length-1,c.compareEndPoints("EndToEnd",e)>-1?h=d:(h=-c.moveEnd("character",-d),h+=a.slice(0,h).split("\n").length-1)))),{start:g,end:h}}function c(){var a=!(w.val().length>=w.attr("maxlength")&&w.attr("maxlength")>=0),c=b(),d=c.start,e=c.end,f=c.start!==c.end&&w.val().substring(d,e).match(/\d/)?!0:!1,g="0"===w.val().substring(0,1);return a||f||g}function d(a){w.each(function(b,c){if(c.setSelectionRange)c.focus(),c.setSelectionRange(a,a);else if(c.createTextRange){var d=c.createTextRange();d.collapse(!0),d.moveEnd("character",a),d.moveStart("character",a),d.select()}})}function e(b){var c="";return b.indexOf("-")>-1&&(b=b.replace("-",""),c="-"),c+a.prefix+b+a.suffix}function f(b){var c,d=b.indexOf("-")>-1&&a.allowNegative?"-":"",g=b.indexOf(a.decimal),h=(-1===g?b:b.slice(0,g)).replace(/\D/g,""),i=(-1===g?"":b.slice(g+1)).replace(/\D/g,"");if(h=h.replace(/^0+/g,""),""===h&&(h="0"),c=d+h,-1!==g&&a.precision>0){var j="";if(i.length>a.precision&&(j=parseFloat(i.substring(a.precision)),i=i.substring(0,a.precision)),c+=a.decimal+i,0===parseFloat(c,10)&&j>0)return f(j.toString())}return e(c)}function g(b){var c,d,f,g=b.indexOf("-")>-1&&a.allowNegative?"-":"",h=b.replace(/[^0-9]/g,""),i=h.slice(0,h.length-a.precision);return i=i.replace(/^0*/g,""),i=i.replace(/\B(?=(\d{3})+(?!\d))/g,a.thousands),""===i&&(i="0"),c=g+i,a.precision>0&&(d=h.slice(h.length-a.precision),f=new Array(a.precision+1-d.length).join(0),c+=a.decimal+f+d),e(c)}function h(a){var b,c=w.val().length;w.val(g(w.val())),b=w.val().length,a-=c-b,d(a)}function i(){var a=w.val();w.val(g(a))}function j(){var b=w.val();return a.allowNegative?""!==b&&"-"===b.charAt(0)?b.replace("-",""):"-"+b:b}function k(a){a.preventDefault?a.preventDefault():a.returnValue=!1}function l(a){a=a||window.event;var d,e,f,g,i,l=a.which||a.charCode||a.keyCode;return void 0===l?!1:0===l?!0:48>l||l>57?45===l?(w.val(j()),!1):43===l?(w.val(w.val().replace("-","")),!1):13===l||9===l?!0:!$.browser.mozilla||37!==l&&39!==l||0!==a.charCode?(k(a),!0):!0:c()?(k(a),d=String.fromCharCode(l),e=b(),f=e.start,g=e.end,i=w.val(),w.val(i.substring(0,f)+d+i.substring(g,i.length)),h(f+1),!1):!1}function m(c){c=c||window.event;var d,e,f,g,i,j=c.which||c.charCode||c.keyCode;return void 0===j?!1:(d=b(),e=d.start,f=d.end,8===j||46===j||63272===j?(k(c),g=w.val(),e===f&&(8===j?""===a.suffix?e-=1:(i=g.split("").reverse().join("").search(/\d/),e=g.length-i-1,f=e+1):f+=1),w.val(g.substring(0,e)+g.substring(f,g.length)),h(e),!1):9===j?!0:!0)}function n(){var a=w.val(),b=f(a);a!==b&&w.val(b)}function o(){p(),setTimeout(function(){w.select()},50)}function p(){v=w.val(),i();var a,b=w.get(0);b.createTextRange&&(a=b.createTextRange(),a.collapse(!1),a.select())}function q(){setTimeout(function(){i()},0)}function r(){var b=parseFloat("0")/Math.pow(10,a.precision);return b.toFixed(a.precision).replace(new RegExp("\\.","g"),a.decimal)}function s(a){i(),t(a)}function t(b){if($.browser.msie&&l(b),""===w.val()||w.val()===e(r()))w.val(a.allowZero?a.affixesStay?e(r()):r():"");else if(!a.affixesStay){var c=w.val().replace(a.prefix,"").replace(a.suffix,"");w.val(c)}w.val()!==v&&w.change()}function u(){var a,b=w.get(0);b.setSelectionRange?(a=w.val().length,b.setSelectionRange(a,a)):w.val(w.val())}var v,w=$(this);a=$.extend(a,w.data()),w.unbind(".maskMoney"),$.browser.chrome&&$.platform.android?(w.bind("keyup.maskMoney",n),w.bind("blur.maskMoney",s),w.bind("focus.maskMoney",o)):(w.bind("keypress.maskMoney",l),w.bind("keydown.maskMoney",m),w.bind("blur.maskMoney",t),w.bind("focus.maskMoney",p),w.bind("click.maskMoney",u)),w.bind("cut.maskMoney",q),w.bind("paste.maskMoney",q),w.bind("mask.maskMoney",i)})}};$.fn.maskMoney=function(a){return e[a]?e[a].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof a&&a?void $.error("Method "+a+" does not exist on jQuery.maskMoney"):e.init.apply(this,arguments)}}(window.jQuery||window.Zepto); \ No newline at end of file diff --git a/src/jquery.maskMoney.js b/src/jquery.maskMoney.js index 889ac5f..f57139b 100644 --- a/src/jquery.maskMoney.js +++ b/src/jquery.maskMoney.js @@ -1,11 +1,78 @@ +/* + * jquery-maskmoney - v3.0.2 + * jQuery plugin to mask data entry in the input text in the form of money (currency) + * https://github.com/plentz/jquery-maskmoney + * + * Made by Diego Plentz + * Under MIT License (https://raw.github.com/plentz/jquery-maskmoney/master/LICENSE) + */ (function ($) { "use strict"; + if (!$.browser) { $.browser = {}; - $.browser.mozilla = /mozilla/.test(navigator.userAgent.toLowerCase()) && !/webkit/.test(navigator.userAgent.toLowerCase()); - $.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase()); - $.browser.opera = /opera/.test(navigator.userAgent.toLowerCase()); - $.browser.msie = /msie/.test(navigator.userAgent.toLowerCase()); + var browserMatched = uaMatchBrowser(navigator.userAgent); + + if (browserMatched.browser) { + $.browser[browserMatched.browser] = true; + $.browser.version = browserMatched.version; + } + + // Chrome is Webkit, but Webkit is also Safari. + if ($.browser.chrome) { + $.browser.webkit = true; + } else if ($.browser.webkit) { + $.browser.safari = true; + } + } + + if (!$.platform) { + $.platform = {}; + var platformMatched = uaMatchPlatform(navigator.userAgent); + + if(platformMatched.platform) { + $.platform[platformMatched.platform] = true; + } + + if(platformMatched.mobile) { + $.platform.mobile = true; + $.platform[platformMatched.mobile] = true; + } + } + + // jQuery Migration library code + function uaMatchBrowser(ua) { + ua = ua.toLowerCase(); + + var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) || + /(webkit)[ \/]([\w.]+)/.exec( ua ) || + /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) || + /(msie) ([\w.]+)/.exec( ua ) || + ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) || + []; + + return { + browser: match[ 1 ] || "", + version: match[ 2 ] || "0" + }; + } + + function uaMatchPlatform(ua) { + ua = ua.toLowerCase(); + + var platformMatch = /(mac)/.exec(ua) || + /(linux)/.exec(ua) || + /(windows)/.exec(ua) || + []; + + var mobileMatch = /(android)/.exec(ua) || + /(iphone)/.exec(ua) || + []; + + return { + platform: platformMatch[1] || "", + mobile: mobileMatch[1] || "" + }; } var methods = { @@ -155,6 +222,43 @@ return operator + settings.prefix + value + settings.suffix; } + function formatValue(value) { + var negative = (value.indexOf("-") > -1 && settings.allowNegative) ? "-" : "", + decimalIndex = value.indexOf(settings.decimal), + integerPart = (decimalIndex === -1 ? value : value.slice(0, decimalIndex)).replace(/\D/g, ""), + decimalPart = (decimalIndex === -1 ? "" : value.slice(decimalIndex+1)).replace(/\D/g, ""), + newValue; + + // remove initial zeros + integerPart = integerPart.replace(/^0+/g, ""); + + if (integerPart === "") { + integerPart = "0"; + } + + newValue = negative + integerPart; + + if ((decimalIndex !== -1) && (settings.precision > 0)) { + var decimalRemoved = ""; + + if(decimalPart.length > settings.precision) { + decimalRemoved = parseFloat(decimalPart.substring(settings.precision)); + decimalPart = decimalPart.substring(0, settings.precision); + } + + newValue += settings.decimal + decimalPart; + + if((parseFloat(newValue, 10) === 0) && (decimalRemoved > 0)) { + // Format the current number typed by the user as an integer part + // when cursor positioned on the right side and the current value + // is '0.00', considereing two decimal places (0.005 > 5). + return formatValue(decimalRemoved.toString()); + } + } + + return setSymbol(newValue); + } + function maskValue(value) { var negative = (value.indexOf("-") > -1 && settings.allowNegative) ? "-" : "", onlyNumbers = value.replace(/[^0-9]/g, ""), @@ -165,6 +269,7 @@ // remove initial zeros integerPart = integerPart.replace(/^0*/g, ""); + // put settings.thousands every 3 chars integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, settings.thousands); if (integerPart === "") { @@ -230,6 +335,9 @@ //added to handle an IE "special" event if (key === undefined) { return false; + } else if(key === 0) { + // Chrome Mobile doesn't provides the key code + return true; } // any key except the numbers 0-9 @@ -319,6 +427,26 @@ } } + // It's just a workaround for Android devices because Chrome + // mobile doesn't support `keypress` event either provides the + // key code on `keydown`, `keyup` or `compositionupdate` events + function keyupEvent() { + var value = $input.val(); + var formattedValue = formatValue(value); + + if(value !== formattedValue) { + $input.val(formattedValue); + } + } + + function focusAndSelect() { + focusEvent(); + setTimeout(function() { + $input.select(); + }, 50); + + } + function focusEvent() { onFocusValue = $input.val(); mask(); @@ -342,6 +470,11 @@ return (n.toFixed(settings.precision)).replace(new RegExp("\\.", "g"), settings.decimal); } + function maskAndBlur(e) { + mask(); + blurEvent(e); + } + function blurEvent(e) { if ($.browser.msie) { keypressEvent(e); @@ -378,11 +511,19 @@ } $input.unbind(".maskMoney"); - $input.bind("keypress.maskMoney", keypressEvent); - $input.bind("keydown.maskMoney", keydownEvent); - $input.bind("blur.maskMoney", blurEvent); - $input.bind("focus.maskMoney", focusEvent); - $input.bind("click.maskMoney", clickEvent); + + if($.browser.chrome && $.platform.android) { + $input.bind("keyup.maskMoney", keyupEvent); + $input.bind("blur.maskMoney", maskAndBlur); + $input.bind("focus.maskMoney", focusAndSelect); + } else { + $input.bind("keypress.maskMoney", keypressEvent); + $input.bind("keydown.maskMoney", keydownEvent); + $input.bind("blur.maskMoney", blurEvent); + $input.bind("focus.maskMoney", focusEvent); + $input.bind("click.maskMoney", clickEvent); + } + $input.bind("cut.maskMoney", cutPasteEvent); $input.bind("paste.maskMoney", cutPasteEvent); $input.bind("mask.maskMoney", mask);