jquery.inputmask.numeric.extensions.js 5.3 KB

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