jquery.inputmask.numeric.extensions.js 7.0 KB

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