jquery.inputmask.numeric.extensions.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - 2012 Robin Herbots
  5. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. Version: 1.3.43
  7. Optional extensions on the jquery.inputmask base
  8. */
  9. (function ($) {
  10. //number aliases
  11. $.extend($.inputmask.defaults.aliases, {
  12. 'decimal': {
  13. mask: "~",
  14. placeholder: "",
  15. repeat: 10,
  16. greedy: false,
  17. numericInput: true,
  18. digits: "*", //numer of digits
  19. groupSeparator: ",", // | "."
  20. groupSize: 3,
  21. autoGroup: false,
  22. postFormat: function (buffer, pos, reformatOnly, opts) {
  23. var cbuf = buffer.slice();
  24. if (!reformatOnly) cbuf.splice(pos, 0, "?"); //set position indicator
  25. var bufVal = cbuf.join('');
  26. if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
  27. bufVal = bufVal.replace(new RegExp("\\" + opts.groupSeparator, "g"), '');
  28. var reg = new RegExp('(-?[\\d?]+)([\\d?]{' + opts.groupSize + '})');
  29. while (reg.test(bufVal)) {
  30. bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
  31. }
  32. }
  33. buffer.length = bufVal.length; //align the length
  34. for (var i = 0, l = bufVal.length; i < l; i++) {
  35. buffer[i] = bufVal.charAt(i);
  36. }
  37. var newPos = reformatOnly ? pos : $.inArray("?", buffer);
  38. if (!reformatOnly) buffer.splice(newPos, 1);
  39. return newPos;
  40. },
  41. regex: {
  42. number: function (groupSeparator, groupSize, radixPoint, digits) {
  43. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, groupSeparator);
  44. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, radixPoint);
  45. var digitExpression = isNaN(digits) ? digits : '{0,' + digits + '}';
  46. return new RegExp("^[\+-]?(\\d+|\\d{1," + groupSize + "}((" + escapedGroupSeparator + "\\d{" + groupSize + "})?)+)(" + escapedRadixPoint + "\\d" + digitExpression + ")?$");
  47. }
  48. },
  49. onKeyDown: function (e, opts) {
  50. var $input = $(this), input = this;
  51. if (e.keyCode == opts.keyCode.TAB) {
  52. var nptStr = input._valueGet();
  53. var radixPosition = nptStr.indexOf(opts.radixPoint);
  54. if (radixPosition != -1) {
  55. for (var i = 1; i < opts.digits; i++) {
  56. if (nptStr[radixPosition + i]) nptStr = nptStr + "0";
  57. }
  58. if (nptStr !== $input.val()) {
  59. $input.val(nptStr);
  60. }
  61. }
  62. } else if (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) {
  63. var nptStr = input._valueGet(),
  64. buffer = nptStr.split('');
  65. var newPos = opts.postFormat(buffer, 0, true, opts);
  66. nptStr = buffer.join('');
  67. input._valueSet(nptStr);
  68. }
  69. },
  70. definitions: {
  71. '~': { //real number
  72. validator: function (chrs, buffer, pos, strict, opts) {
  73. if (chrs == "") return false;
  74. if (pos == 1 && buffer[0] === '0' && new RegExp("[\\d|-]").test(chrs)) { //handle first char
  75. buffer[0] = "";
  76. return { "pos": 0 };
  77. }
  78. var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
  79. cbuf.splice(pos, 0, chrs);
  80. var bufferStr = cbuf.join('');
  81. if (opts.autoGroup) //strip groupseparator
  82. bufferStr = bufferStr.replace(new RegExp("\\" + opts.groupSeparator, "g"), '');
  83. var isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  84. if (!isValid) {
  85. //let's help the regex a bit
  86. bufferStr += "0";
  87. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  88. if (!isValid) {
  89. //make a valid group
  90. var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
  91. for (i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
  92. bufferStr += "0";
  93. }
  94. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  95. if (!isValid && !strict) {
  96. if (chrs == opts.radixPoint) {
  97. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test("0" + bufferStr + "0");
  98. if (isValid) {
  99. buffer[pos] = "0";
  100. pos++;
  101. return { "pos": pos };
  102. }
  103. }
  104. }
  105. }
  106. }
  107. if (isValid != false && !strict) {
  108. var newPos = opts.postFormat(buffer, pos, false, opts);
  109. return { "pos": newPos };
  110. }
  111. return isValid;
  112. },
  113. cardinality: 1,
  114. prevalidator: null
  115. }
  116. },
  117. insertMode: true,
  118. autoUnmask: false
  119. },
  120. 'non-negative-decimal': {
  121. regex: {
  122. number: function (groupSeparator, groupSize, radixPoint, digits) {
  123. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, groupSeparator);
  124. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, radixPoint);
  125. var digitExpression = isNaN(digits) ? digits : '{0,' + digits + '}'
  126. return new RegExp("^[\+]?(\\d+|\\d{1," + groupSize + "}((" + escapedGroupSeparator + "\\d{" + groupSize + "})?)+)(" + escapedRadixPoint + "\\d" + digitExpression + ")?$");
  127. }
  128. },
  129. alias: "decimal"
  130. },
  131. 'integer': {
  132. regex: {
  133. number: function (groupSeparator, groupSize) { return new RegExp("^([\+\-]?\\d*)$"); }
  134. },
  135. alias: "decimal"
  136. }
  137. });
  138. })(jQuery);