forked from price-format/Jquery-Price-Format
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.price_format.min.js
3 lines (3 loc) · 1.4 KB
/
jquery.price_format.min.js
1
2
3
(function(d){d.fn.priceFormat=function(c){c=d.extend({prefix:"US$ ",centsSeparator:".",thousandsSeparator:",",limit:!1,centsLimit:2,clearPrefix:!1,allowNegative:!1},c);return this.each(function(){function k(e){for(var a="",g=0;g<e.length;g++)char_=e.charAt(g),0==a.length&&0==char_&&(char_=!1),char_&&char_.match(r)&&(l?a.length<l&&(a+=char_):a+=char_);for(var b;a.length<i+1;)a="0"+a;b=a;var a="",g=0,d=b.substr(b.length-i,i),c=b.substr(0,b.length-i);b=c+m+d;if(j){for(b=c.length;0<b;b--)char_=c.substr(b-
1,1),g++,0==g%3&&(char_=j+char_),a=char_+a;a.substr(0,1)==j&&(a=a.substring(1,a.length));b=a+m+d}if(n&&-1!=e.indexOf("-")&&(0!=c||0!=d))b="-"+b;h&&(b=h+b);return b}function o(){var e=f.val(),a=k(e);e!=a&&f.val(a)}function p(){if(""!=d.trim(h)&&q){var e=f.val().split(h);f.val(e[1])}}var f=d(this),r=/[0-9]/,h=c.prefix,m=c.centsSeparator,j=c.thousandsSeparator,l=c.limit,i=c.centsLimit,q=c.clearPrefix,n=c.allowNegative;d(this).bind("keydown",function(e){var a=e.keyCode?e.keyCode:e.which,c=String.fromCharCode(a),
b=!1,d=f.val(),c=k(d+c);if(48<=a&&57>=a||96<=a&&105>=a)b=!0;8==a&&(b=!0);9==a&&(b=!0);13==a&&(b=!0);46==a&&(b=!0);37==a&&(b=!0);39==a&&(b=!0);if(n&&(189==a||109==a))b=!0;b||(e.preventDefault(),e.stopPropagation(),d!=c&&f.val(c))});d(this).bind("keyup",o);q&&(d(this).bind("focusout",function(){p()}),d(this).bind("focusin",function(){var c=f.val();f.val(h+c)}));0<d(this).val().length&&(o(),p())})}})(jQuery);