forked from price-format/Jquery-Price-Format
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.priceformat.min.js
1 lines (1 loc) · 3.1 KB
/
jquery.priceformat.min.js
1
!function(a){a.fn.priceFormat=function(b){var b=a.extend(!0,{},a.fn.priceFormat.defaults,b);return window.ctrl_down=!1,metaKey=!1,a(window).bind("keyup keydown",function(a){return window.ctrl_down=a.ctrlKey,!0}),a(this).bind("keyup keydown",function(a){return metaKey=a.metaKey,!0}),this.each(function(){function r(a){c.is("input")?c.val(a):c.html(a),c.trigger("pricechange")}function s(){return d=c.is("input")?c.val():c.html()}function t(a){for(var b="",c=0;c<a.length;c++)char_=a.charAt(c),0==b.length&&0==char_&&(char_=!1),char_&&char_.match(e)&&(j?b.length<j&&(b+=char_):b+=char_);return b}function u(a){for(;a.length<k+1;)a="0"+a;return a}function v(b,c){if(!c&&(""===b||b==v("0",!0))&&p)return"";var d=u(t(b)),e="",j=0;0==k&&(h="",l="");var l=d.substr(d.length-k,k),m=d.substr(0,d.length-k);if(d=q?m+h+l:"0"!==m?m+h+l:h+l,i||""!=a.trim(i)){for(var r=m.length;r>0;r--)char_=m.substr(r-1,1),j++,j%3==0&&(char_=i+char_),e=char_+e;e.substr(0,1)==i&&(e=e.substring(1,e.length)),d=0==k?e:e+h+l}return!n||0==m&&0==l||(d=b.indexOf("-")!=-1&&b.indexOf("+")<b.indexOf("-")?"-"+d:o?"+"+d:""+d),f&&(d=f+d),g&&(d+=g),d}function w(a){var b=a.keyCode?a.keyCode:a.which,c=String.fromCharCode(b),e=!1,f=d,g=v(f+c);(b>=48&&b<=57||b>=96&&b<=105)&&(e=!0),192==b&&(e=!0),8==b&&(e=!0),9==b&&(e=!0),13==b&&(e=!0),46==b&&(e=!0),37==b&&(e=!0),39==b&&(e=!0),!n||189!=b&&109!=b&&173!=b||(e=!0),!o||187!=b&&107!=b&&61!=b||(e=!0),b>=16&&b<=20&&(e=!0),27==b&&(e=!0),b>=33&&b<=40&&(e=!0),b>=44&&b<=46&&(e=!0),(window.ctrl_down||metaKey)&&(86==b&&(e=!0),67==b&&(e=!0),88==b&&(e=!0),82==b&&(e=!0),84==b&&(e=!0),76==b&&(e=!0),87==b&&(e=!0),81==b&&(e=!0),78==b&&(e=!0),65==b&&(e=!0)),e||(a.preventDefault(),a.stopPropagation(),f!=g&&r(g))}function x(){var a=s(),b=v(a);a!=b&&r(b);var d=v("0",!0);b==d&&"0"!=a&&p&&r(""),c.change()}function y(){c.val(f+s())}function z(){c.val(s()+g)}function A(){if(""!=a.trim(f)&&l){var b=s().split(f);r(b[1])}}function B(){if(""!=a.trim(g)&&m){var b=s().split(g);r(b[0])}}var c=a(this),d="",e=/[0-9]/;d=c.is("input")?c.val():c.html();var f=b.prefix,g=b.suffix,h=b.centsSeparator,i=b.thousandsSeparator,j=b.limit,k=b.centsLimit,l=b.clearPrefix,m=b.clearSuffix,n=b.allowNegative,o=b.insertPlusSign,p=b.clearOnEmpty,q=b.leadingZero;o&&(n=!0),c.bind("keydown.price_format",w),c.bind("keyup.price_format",x),c.bind("focusout.price_format",x),l&&(c.bind("focusout.price_format",function(){A()}),c.bind("focusin.price_format",function(){y()})),m&&(c.bind("focusout.price_format",function(){B()}),c.bind("focusin.price_format",function(){z()})),s().length>0&&(x(),A(),B())})},a.fn.unpriceFormat=function(){return a(this).unbind(".price_format")},a.fn.unmask=function(){var b,c="";b=a(this).is("input")?a(this).val()||[]:a(this).html();for(var d=0;d<b.length;d++)isNaN(b[d])&&"-"!=b[d]||(c+=b[d]);return c},a.fn.priceToFloat=function(){return a(this).is("input")?field=a(this).val()||[]:field=a(this).html(),parseFloat(field.replace(/[^0-9\-\.]/g,""))},a.fn.priceFormat.defaults={prefix:"US$ ",suffix:"",centsSeparator:".",thousandsSeparator:",",limit:!1,centsLimit:2,clearPrefix:!1,clearSufix:!1,allowNegative:!1,insertPlusSign:!1,clearOnEmpty:!1,leadingZero:!0}}(jQuery);