You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.9 KiB

4 years ago
  1. /*
  2. * Price Format jQuery Plugin
  3. * Created By Eduardo Cuducos cuducos [at] gmail [dot] com
  4. * Currently maintained by Flavio Silveira flavio [at] gmail [dot] com
  5. * Version: 1.7
  6. * Release: 2012-02-22
  7. * original char limit by Flavio Silveira <http://flaviosilveira.com>
  8. * original keydown event attachment by Kaihua Qi
  9. * keydown fixes by Thasmo <http://thasmo.com>
  10. * Clear Prefix on Blur suggest by Ricardo Mendes from PhonoWay
  11. * original allow negative by Cagdas Ucar <http://carsinia.com>
  12. * keypad fixes by Carlos Vinicius <http://www.kvinicius.com.br> and Rayron Victor
  13. * original Suffix by Marlon Pires Junior
  14. */
  15. (function ($) {
  16. /****************
  17. * Main Function *
  18. *****************/
  19. $.fn.priceFormat = function (options) {
  20. var defaults =
  21. {
  22. prefix: 'US$ ',
  23. suffix: '',
  24. centsSeparator: '.',
  25. thousandsSeparator: ',',
  26. limit: false,
  27. centsLimit: 2,
  28. clearPrefix: false,
  29. clearSufix: false,
  30. allowNegative: false
  31. };
  32. var options = $.extend(defaults, options);
  33. return this.each(function () {
  34. // pre defined options
  35. var obj = $(this);
  36. var is_number = /[0-9]/;
  37. // load the pluggings settings
  38. var prefix = options.prefix;
  39. var suffix = options.suffix;
  40. var centsSeparator = options.centsSeparator;
  41. var thousandsSeparator = options.thousandsSeparator;
  42. var limit = options.limit;
  43. var centsLimit = options.centsLimit;
  44. var clearPrefix = options.clearPrefix;
  45. var clearSuffix = options.clearSuffix;
  46. var allowNegative = options.allowNegative;
  47. // skip everything that isn't a number
  48. // and also skip the left zeroes
  49. function to_numbers(str) {
  50. var formatted = '';
  51. for (var i = 0; i < (str.length); i++) {
  52. char_ = str.charAt(i);
  53. if (formatted.length == 0 && char_ == 0) char_ = false;
  54. if (char_ && char_.match(is_number)) {
  55. if (limit) {
  56. if (formatted.length < limit) formatted = formatted + char_;
  57. }
  58. else {
  59. formatted = formatted + char_;
  60. }
  61. }
  62. }
  63. return formatted;
  64. }
  65. // format to fill with zeros to complete cents chars
  66. function fill_with_zeroes(str) {
  67. while (str.length < (centsLimit + 1)) str = '0' + str;
  68. return str;
  69. }
  70. // format as price
  71. function price_format(str) {
  72. // formatting settings
  73. var formatted = fill_with_zeroes(to_numbers(str));
  74. var thousandsFormatted = '';
  75. var thousandsCount = 0;
  76. // split integer from cents
  77. var centsVal = formatted.substr(formatted.length - centsLimit, centsLimit);
  78. var integerVal = formatted.substr(0, formatted.length - centsLimit);
  79. // apply cents pontuation
  80. formatted = integerVal + centsSeparator + centsVal;
  81. // apply thousands pontuation
  82. if (thousandsSeparator) {
  83. for (var j = integerVal.length; j > 0; j--) {
  84. char_ = integerVal.substr(j - 1, 1);
  85. thousandsCount++;
  86. if (thousandsCount % 3 == 0) char_ = thousandsSeparator + char_;
  87. thousandsFormatted = char_ + thousandsFormatted;
  88. }
  89. if (thousandsFormatted.substr(0, 1) == thousandsSeparator) thousandsFormatted = thousandsFormatted.substring(1, thousandsFormatted.length);
  90. formatted = thousandsFormatted + centsSeparator + centsVal;
  91. }
  92. // if the string contains a dash, it is negative - add it to the begining (except for zero)
  93. if (allowNegative && str.indexOf('-') != -1 && (integerVal != 0 || centsVal != 0)) formatted = '-' + formatted;
  94. // apply the prefix
  95. if (prefix) formatted = prefix + formatted;
  96. // apply the suffix
  97. if (suffix) formatted = formatted + suffix;
  98. return formatted;
  99. }
  100. // filter what user type (only numbers and functional keys)
  101. function key_check(e) {
  102. var code = (e.keyCode ? e.keyCode : e.which);
  103. var typed = String.fromCharCode(code);
  104. var functional = false;
  105. var str = obj.val();
  106. var newValue = price_format(str + typed);
  107. // allow key numbers, 0 to 9
  108. if ((code >= 48 && code <= 57) || (code >= 96 && code <= 105)) functional = true;
  109. // check Backspace, Tab, Enter, Delete, and left/right arrows
  110. if (code == 8) functional = true;
  111. if (code == 9) functional = true;
  112. if (code == 13) functional = true;
  113. if (code == 46) functional = true;
  114. if (code == 37) functional = true;
  115. if (code == 39) functional = true;
  116. if (allowNegative && (code == 189 || code == 109)) functional = true; // dash as well
  117. if (!functional) {
  118. e.preventDefault();
  119. e.stopPropagation();
  120. if (str != newValue) obj.val(newValue);
  121. }
  122. }
  123. // inster formatted price as a value of an input field
  124. function price_it() {
  125. var str = obj.val();
  126. var price = price_format(str);
  127. if (str != price) obj.val(price);
  128. }
  129. // Add prefix on focus
  130. function add_prefix() {
  131. var val = obj.val();
  132. obj.val(prefix + val);
  133. }
  134. function add_suffix() {
  135. var val = obj.val();
  136. obj.val(val + suffix);
  137. }
  138. // Clear prefix on blur if is set to true
  139. function clear_prefix() {
  140. if ($.trim(prefix) != '' && clearPrefix) {
  141. var array = obj.val().split(prefix);
  142. obj.val(array[1]);
  143. }
  144. }
  145. // Clear suffix on blur if is set to true
  146. function clear_suffix() {
  147. if ($.trim(suffix) != '' && clearSuffix) {
  148. var array = obj.val().split(suffix);
  149. obj.val(array[0]);
  150. }
  151. }
  152. // bind the actions
  153. $(this).bind('keydown', key_check);
  154. $(this).bind('keyup', price_it);
  155. // Clear Prefix and Add Prefix
  156. if (clearPrefix) {
  157. $(this).bind('focusout', function () {
  158. clear_prefix();
  159. });
  160. $(this).bind('focusin', function () {
  161. add_prefix();
  162. });
  163. }
  164. // Clear Suffix and Add Suffix
  165. if (clearSuffix) {
  166. $(this).bind('focusout', function () {
  167. clear_suffix();
  168. });
  169. $(this).bind('focusin', function () {
  170. add_suffix();
  171. });
  172. }
  173. // If value has content
  174. if ($(this).val().length > 0) {
  175. price_it();
  176. clear_prefix();
  177. clear_suffix();
  178. }
  179. });
  180. };
  181. /******************
  182. * Unmask Function *
  183. *******************/
  184. jQuery.fn.unmask = function () {
  185. var field = $(this).val();
  186. var result = "";
  187. for (var f in field) {
  188. if (!isNaN(field[f]) || field[f] == "-") result += field[f];
  189. }
  190. return result;
  191. };
  192. })(jQuery);