jquery.inputmask.numeric.extentions.js 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.2
  7. Optional extentions on the jquery.inputmask base
  8. */
  9. (function($) {
  10. //number aliases
  11. $.extend($.inputmask.defaults, {
  12. radixPoint: "\.", // | ","
  13. digits: "*", //numer of digits
  14. groupSeparator: ",", // | "\."
  15. groupSize: 3
  16. });
  17. $.extend($.inputmask.defaults.aliases, {
  18. 'decimal': {
  19. mask: "~",
  20. placeholder: "",
  21. repeat: 10,
  22. greedy: false,
  23. numericInput: true,
  24. regex: {
  25. number: function(radixPoint, digits) { return new RegExp("^[\+\\d\-]{1}\\d*[" + radixPoint + "]?\\d" + digits + "$"); }
  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[opts.radixPoint.length -1]);
  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. var cbuf = buffer.slice();
  47. cbuf.splice(pos, 0, chrs);
  48. var bufferStr = cbuf.join('');
  49. var isValid = opts.regex.number(opts.radixPoint, digitExpression()).test(bufferStr);
  50. if (!isValid) {
  51. if (strict) { //shiftL & shiftR use strict only validate from 0 to position
  52. var cbuf = buffer.slice(0, pos);
  53. cbuf.splice(pos, 0, chrs);
  54. var bufferStr = cbuf.join('');
  55. var isValid = opts.regex.number(opts.radixPoint, digitExpression()).test(bufferStr);
  56. }
  57. else {
  58. if (bufferStr == opts.radixPoint) {
  59. isValid = opts.regex.number(opts.radixPoint, digitExpression()).test("0" + bufferStr);
  60. if (isValid) {
  61. buffer[pos] = "0";
  62. pos++;
  63. return pos;
  64. }
  65. }
  66. }
  67. }
  68. //todo grouping, radixpoint positioning
  69. return isValid;
  70. },
  71. cardinality: 1,
  72. prevalidator: null
  73. }
  74. },
  75. insertMode: true
  76. },
  77. 'non-negative-decimal': {
  78. regex: {
  79. number: function(radixPoint, digits) { return new RegExp("^\\d+[" + radixPoint + "]?\\d" + digits + "$"); }
  80. },
  81. alias: "decimal"
  82. },
  83. 'integer': {
  84. regex: {
  85. number: function() { return new RegExp("^([\+\-]?\\d*)$"); }
  86. },
  87. alias: "decimal"
  88. }
  89. });
  90. })(jQuery);