jquery.inputmask.numeric.extensions.js 6.3 KB

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