jquery.inputmask.numeric.extentions.js 5.1 KB

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