jquery.inputmask.numeric.extensions.js 5.4 KB

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