jquery.inputmask.numeric.extensions.js 5.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.0.4
  7. Optional extensions on the jquery.inputmask base
  8. */
  9. (function($) {
  10. //number aliases
  11. //$.extend($.inputmask.defaults.definitions, {
  12. // '9': {
  13. // validator: function(chrs, buffer, pos, strict, opts) {
  14. // var isValid = opts.definitions['9'].regex.test(chrs);
  15. // if (isValid) {
  16. //do some grouping
  17. // }
  18. // return isValid;
  19. // },
  20. // cardinality: 1,
  21. // regex: new RegExp("[0-9]")
  22. // }
  23. //});
  24. $.extend($.inputmask.defaults.aliases, {
  25. 'decimal': {
  26. mask: "~",
  27. placeholder: "",
  28. repeat: 10,
  29. greedy: false,
  30. numericInput: true,
  31. regex: {
  32. number: function(radixPoint, digits) { return new RegExp("^[\+\\d\-]{1}\\d*[" + radixPoint + "]?\\d" + digits + "$"); }
  33. },
  34. onKeyDown: function(e, opts) {
  35. var $input = $(this), input = this;
  36. if (e.keyCode == opts.keyCode.TAB) {
  37. var nptStr = input._valueGet();
  38. var radixPosition = nptStr.indexOf(opts.radixPoint[opts.radixPoint.length - 1]);
  39. if (radixPosition != -1) {
  40. for (var i = 1; i < opts.digits; i++) {
  41. if (nptStr[radixPosition + i]) nptStr = nptStr + "0";
  42. }
  43. $input.val(nptStr);
  44. }
  45. }
  46. },
  47. definitions: {
  48. '~': { //real number
  49. validator: function(chrs, buffer, pos, strict, opts) {
  50. function digitExpression() {
  51. return isNaN(opts.digits) ? opts.digits : '{0,' + opts.digits + '}';
  52. }
  53. var cbuf = buffer.slice();
  54. cbuf.splice(pos, 0, chrs);
  55. var bufferStr = cbuf.join('');
  56. var isValid = opts.regex.number(opts.radixPoint, digitExpression()).test(bufferStr);
  57. if (!isValid) {
  58. if (strict) { //shiftL & shiftR use strict only validate from 0 to position
  59. var cbuf = buffer.slice(0, pos);
  60. cbuf.splice(pos, 0, chrs);
  61. var bufferStr = cbuf.join('');
  62. var isValid = opts.regex.number(opts.radixPoint, digitExpression()).test(bufferStr);
  63. }
  64. else {
  65. if (bufferStr == opts.radixPoint) {
  66. isValid = opts.regex.number(opts.radixPoint, digitExpression()).test("0" + bufferStr);
  67. if (isValid) {
  68. buffer[pos] = "0";
  69. pos++;
  70. return { "pos": pos };
  71. }
  72. }
  73. }
  74. }
  75. //todo grouping, radixpoint positioning
  76. return isValid;
  77. },
  78. cardinality: 1,
  79. prevalidator: null
  80. }
  81. },
  82. insertMode: true
  83. },
  84. 'non-negative-decimal': {
  85. regex: {
  86. number: function(radixPoint, digits) { return new RegExp("^\\d+[" + radixPoint + "]?\\d" + digits + "$"); }
  87. },
  88. alias: "decimal"
  89. },
  90. 'integer': {
  91. regex: {
  92. number: function() { return new RegExp("^([\+\-]?\\d*)$"); }
  93. },
  94. alias: "decimal"
  95. }
  96. });
  97. })(jQuery);