jquery.inputmask.numeric.extensions.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.3b
  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 || 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 : buffer.indexOf("?");
  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. var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
  75. cbuf.splice(pos, 0, chrs);
  76. var bufferStr = cbuf.join('');
  77. if (opts.autoGroup) //strip groupseparator
  78. bufferStr = bufferStr.replace(new RegExp("\\" + opts.groupSeparator, "g"), '');
  79. var isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  80. if (!isValid) {
  81. //let's help the regex a bit
  82. bufferStr += "0";
  83. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  84. if (!isValid) {
  85. //make a valid group
  86. var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
  87. for (i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
  88. bufferStr += "0";
  89. }
  90. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test(bufferStr);
  91. if (!isValid && !strict) {
  92. if (chrs == opts.radixPoint) {
  93. isValid = opts.regex.number(opts.groupSeparator, opts.groupSize, opts.radixPoint, opts.digits).test("0" + bufferStr + "0");
  94. if (isValid) {
  95. buffer[pos] = "0";
  96. pos++;
  97. return { "pos": pos };
  98. }
  99. }
  100. }
  101. }
  102. }
  103. if (isValid != false && !strict) {
  104. var newPos = opts.postFormat(buffer, pos, false, opts);
  105. return { "pos": newPos };
  106. }
  107. return isValid;
  108. },
  109. cardinality: 1,
  110. prevalidator: null
  111. }
  112. },
  113. insertMode: true,
  114. autoUnmask: false
  115. },
  116. 'non-negative-decimal': {
  117. regex: {
  118. number: function (groupSeparator, groupSize, radixPoint, digits) {
  119. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, groupSeparator);
  120. var escapedRadixPoint = $.inputmask.escapeRegex.call(this, radixPoint);
  121. var digitExpression = isNaN(digits) ? digits : '{0,' + digits + '}'
  122. return new RegExp("^[\+]?(\\d+|\\d{1," + groupSize + "}((" + escapedGroupSeparator + "\\d{" + groupSize + "})?)+)(" + escapedRadixPoint + "\\d" + digitExpression + ")?$");
  123. }
  124. },
  125. alias: "decimal"
  126. },
  127. 'integer': {
  128. regex: {
  129. number: function (groupSeparator, groupSize) { return new RegExp("^([\+\-]?\\d*)$"); }
  130. },
  131. alias: "decimal"
  132. }
  133. });
  134. })(jQuery);