diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/jquery.priceformat.js b/jquery.priceformat.js index f90ff55..d6eb2d4 100755 --- a/jquery.priceformat.js +++ b/jquery.priceformat.js @@ -61,6 +61,7 @@ var insertPlusSign = options.insertPlusSign; var clearOnEmpty = options.clearOnEmpty; var leadingZero = options.leadingZero; + var allowZero = options.allowZero; // If insertPlusSign is on, it automatic turns on allowNegative, to work with Signs if (insertPlusSign) allowNegative = true; @@ -111,8 +112,9 @@ // format as price function price_format(str, ignore) { - if (!ignore && (str === '' || str == price_format('0', true)) && clearOnEmpty) - return ''; + if (!ignore && (str === "" || (str === price_format("0", true) && !allowZero)) && clearOnEmpty) { + return ""; + } // formatting settings var formatted = fill_with_zeroes(to_numbers(str)); @@ -233,7 +235,7 @@ var price = price_format(str); if (str != price) set(price); var format = price_format('0', true); - if (price == format && str != '0' && clearOnEmpty) set(''); + if (price == format && str != '0' && clearOnEmpty && !allowZero) set(''); } // Add prefix on focus @@ -356,6 +358,7 @@ allowNegative: false, insertPlusSign: false, clearOnEmpty: false, - leadingZero: true + leadingZero: true, + allowZero: false }; })); diff --git a/jquery.priceformat.min.js b/jquery.priceformat.min.js index e0e4ba3..4b5dbe0 100755 --- a/jquery.priceformat.min.js +++ b/jquery.priceformat.min.js @@ -1 +1 @@ -!function(n){"function"==typeof define&&define.amd?define(["jquery"],n):"undefined"!=typeof exports?module.exports=n(require("jquery")):n(jQuery)}(function(n){"use strict";n.fn.priceFormat=function(t){var t=n.extend(!0,{},n.fn.priceFormat.defaults,t);window.ctrl_down=!1;var i=!1;return n(window).bind("keyup keydown",function(n){return window.ctrl_down=n.ctrlKey,!0}),n(this).bind("keyup keydown",function(n){return i=n.metaKey,!0}),this.each(function(){function r(n){h.is("input")?h.val(n):h.html(n),h.trigger("pricechange")}function e(){return m=h.is("input")?h.val():h.html()}function o(n){for(var t="",i=0;i0;l--){var p=s.substr(l-1,1);a++,a%3==0&&(p=b+p),e=p+e}e.substr(0,1)==b&&(e=e.substring(1,e.length)),r=0==_?e:e+y+c}return!F||0==s&&0==c||(r=-1!=t.indexOf("-")&&t.indexOf("+")=48&&t<=57||t>=96&&t<=105)&&(o=!0),192==t&&(o=!0),8==t&&(o=!0),9==t&&(o=!0),13==t&&(o=!0),46==t&&(o=!0),37==t&&(o=!0),39==t&&(o=!0),!F||189!=t&&109!=t&&173!=t||(o=!0),!O||187!=t&&107!=t&&61!=t||(o=!0),t>=16&&t<=20&&(o=!0),27==t&&(o=!0),t>=33&&t<=40&&(o=!0),t>=44&&t<=46&&(o=!0),(window.ctrl_down||i)&&(86==t&&(o=!0),67==t&&(o=!0),88==t&&(o=!0),82==t&&(o=!0),84==t&&(o=!0),76==t&&(o=!0),87==t&&(o=!0),81==t&&(o=!0),78==t&&(o=!0),65==t&&(o=!0)),o||(n.preventDefault(),n.stopPropagation(),u!=a&&r(a))}function c(){var n=e(),t=f(n);n!=t&&r(t),t==f("0",!0)&&"0"!=n&&P&&r("")}function s(){h.val(g+e())}function l(){h.val(e()+w)}function p(){if(""!=n.trim(g)&&S){r(e().split(g)[1])}}function d(){if(""!=n.trim(w)&&k){r(e().split(w)[0])}}var h=n(this),m="",v=/[0-9]/;m=h.is("input")?h.val():h.html();var g=t.prefix,w=t.suffix,y=t.centsSeparator,b=t.thousandsSeparator,x=t.limit,_=t.centsLimit,S=t.clearPrefix,k=t.clearSuffix,F=t.allowNegative,O=t.insertPlusSign,P=t.clearOnEmpty,C=t.leadingZero;O&&(F=!0),h.bind("keydown.price_format",a),h.bind("keyup.price_format",c),h.bind("focusout.price_format",c),S&&(h.bind("focusout.price_format",function(){p()}),h.bind("focusin.price_format",function(){s()})),k&&(h.bind("focusout.price_format",function(){d()}),h.bind("focusin.price_format",function(){l()})),e().length>0&&(c(),p(),d())})},n.fn.unpriceFormat=function(){return n(this).unbind(".price_format")},n.fn.unmask=function(){var t,i="";t=n(this).is("input")?n(this).val()||[]:n(this).html();for(var r=0;r0;l--){var p=s.substr(l-1,1);f++,f%3==0&&(p=b+p),e=p+e}e.substr(0,1)==b&&(e=e.substring(1,e.length)),r=0==_?e:e+y+c}return!F||0==s&&0==c||(r=-1!=t.indexOf("-")&&t.indexOf("+")=48&&t<=57||t>=96&&t<=105)&&(o=!0),192==t&&(o=!0),8==t&&(o=!0),9==t&&(o=!0),13==t&&(o=!0),46==t&&(o=!0),37==t&&(o=!0),39==t&&(o=!0),!F||189!=t&&109!=t&&173!=t||(o=!0),!O||187!=t&&107!=t&&61!=t||(o=!0),t>=16&&t<=20&&(o=!0),27==t&&(o=!0),t>=33&&t<=40&&(o=!0),t>=44&&t<=46&&(o=!0),(window.ctrl_down||i)&&(86==t&&(o=!0),67==t&&(o=!0),88==t&&(o=!0),82==t&&(o=!0),84==t&&(o=!0),76==t&&(o=!0),87==t&&(o=!0),81==t&&(o=!0),78==t&&(o=!0),65==t&&(o=!0)),o||(n.preventDefault(),n.stopPropagation(),u!=f&&r(f))}function c(){var n=e(),t=a(n);n!=t&&r(t),t==a("0",!0)&&"0"!=n&&P&&!N&&r("")}function s(){h.val(g+e())}function l(){h.val(e()+w)}function p(){if(""!=n.trim(g)&&S){r(e().split(g)[1])}}function d(){if(""!=n.trim(w)&&k){r(e().split(w)[0])}}var h=n(this),m="",v=/[0-9]/;m=h.is("input")?h.val():h.html();var g=t.prefix,w=t.suffix,y=t.centsSeparator,b=t.thousandsSeparator,x=t.limit,_=t.centsLimit,S=t.clearPrefix,k=t.clearSuffix,F=t.allowNegative,O=t.insertPlusSign,P=t.clearOnEmpty,C=t.leadingZero,N=t.allowZero;O&&(F=!0),h.bind("keydown.price_format",f),h.bind("keyup.price_format",c),h.bind("focusout.price_format",c),S&&(h.bind("focusout.price_format",function(){p()}),h.bind("focusin.price_format",function(){s()})),k&&(h.bind("focusout.price_format",function(){d()}),h.bind("focusin.price_format",function(){l()})),e().length>0&&(c(),p(),d())})},n.fn.unpriceFormat=function(){return n(this).unbind(".price_format")},n.fn.unmask=function(){var t,i="";t=n(this).is("input")?n(this).val()||[]:n(this).html();for(var r=0;r0&&(this.priceIt(),this.clearPrefix())},keyCheck:function(t){var e=t.keyCode?t.keyCode:t.which,i=String.fromCharCode(e),r=!1,s=t.target.value,a=this.priceFormat(s+i);(e>=48&&e<=57||e>=96&&e<=105)&&(r=!0),8==e&&(r=!0),9==e&&(r=!0),13==e&&(r=!0),17==e&&(r=!0),46==e&&(r=!0),35==e&&(r=!0),36==e&&(r=!0),37==e&&(r=!0),39==e&&(r=!0),!this.allowNegative||189!=e&&109!=e||(r=!0),window.ctrl_down&&(86==e&&(r=!0),67==e&&(r=!0),88==e&&(r=!0),82==e&&(r=!0),84==e&&(r=!0),76==e&&(r=!0),87==e&&(r=!0),81==e&&(r=!0),78==e&&(r=!0),65==e&&(r=!0)),r||(t.preventDefault(),t.stopPropagation(),s!=a&&(t.target.value=a))},toNumbers:function(t){for(var e="",i=0;i0;h--)char=n.substr(h-1,1),s++,s%3==0&&(char=this.thousandsSeparator+char),r=char+r;r.substr(0,1)==this.thousandsSeparator&&(r=r.substring(1,r.length)),i=r+this.centsSeparator+a}return!this.allowNegative||t.indexOf("-")==-1||0==n&&0==a||(i="-"+i),this.prefix&&(i=this.prefix+i),i},fillWithZeroes:function(t){for(;t.length0&&(this.priceIt(),this.clearPrefix())},keyCheck:function(t){var e=t.keyCode?t.keyCode:t.which,i=String.fromCharCode(e),r=!1,s=t.target.value,a=this.priceFormat(s+i);(e>=48&&e<=57||e>=96&&e<=105)&&(r=!0),8==e&&(r=!0),9==e&&(r=!0),13==e&&(r=!0),17==e&&(r=!0),46==e&&(r=!0),35==e&&(r=!0),36==e&&(r=!0),37==e&&(r=!0),39==e&&(r=!0),!this.allowNegative||189!=e&&109!=e||(r=!0),window.ctrl_down&&(86==e&&(r=!0),67==e&&(r=!0),88==e&&(r=!0),82==e&&(r=!0),84==e&&(r=!0),76==e&&(r=!0),87==e&&(r=!0),81==e&&(r=!0),78==e&&(r=!0),65==e&&(r=!0)),r||(t.preventDefault(),t.stopPropagation(),s!=a&&(t.target.value=a))},toNumbers:function(t){for(var e="",i=0;i0;n--)char=h.substr(n-1,1),s++,s%3==0&&(char=this.thousandsSeparator+char),r=char+r;r.substr(0,1)==this.thousandsSeparator&&(r=r.substring(1,r.length)),i=r+this.centsSeparator+a}return!this.allowNegative||-1==t.indexOf("-")||0==h&&0==a||(i="-"+i),this.prefix&&(i=this.prefix+i),i},fillWithZeroes:function(t){for(;t.length