jquery.inputmask.numeric.extensions.js 7.0 KB

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