jquery.inputmask.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Input Mask plugin for jquery
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 0.0.0-dev
  7. */
  8. (function(factory) {
  9. if (typeof define === "function" && define.amd) {
  10. define(["jquery", "inputmask"], factory);
  11. } else if (typeof exports === "object") {
  12. module.exports = factory(require("jquery"), require("./inputmask"));
  13. } else {
  14. factory(jQuery, window.Inputmask);
  15. }
  16. }
  17. (function($, Inputmask) {
  18. if ($.fn.inputmask === undefined) {
  19. //jquery plugin
  20. $.fn.inputmask = function(fn, options) {
  21. var nptmask, input;
  22. options = options || {};
  23. if (typeof fn === "string") {
  24. switch (fn) {
  25. case "mask":
  26. nptmask = new Inputmask(options);
  27. return this.each(function() {
  28. nptmask.mask(this);
  29. });
  30. case "unmaskedvalue":
  31. input = this.jquery && this.length > 0 ? this[0] : this;
  32. return input.inputmask ? input.inputmask.unmaskedvalue() : $(input).val();
  33. case "remove":
  34. return this.each(function() {
  35. if (this.inputmask) this.inputmask.remove();
  36. });
  37. case "getemptymask":
  38. input = this.jquery && this.length > 0 ? this[0] : this;
  39. return input.inputmask ? input.inputmask.getemptymask() : "";
  40. case "hasMaskedValue": //check wheter the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value
  41. input = this.jquery && this.length > 0 ? this[0] : this;
  42. return input.inputmask ? input.inputmask.hasMaskedValue() : false;
  43. case "isComplete":
  44. input = this.jquery && this.length > 0 ? this[0] : this;
  45. return input.inputmask ? input.inputmask.isComplete() : true;
  46. case "getmetadata": //return mask metadata if exists
  47. input = this.jquery && this.length > 0 ? this[0] : this;
  48. return input.inputmask ? input.inputmask.getmetadata() : undefined;
  49. case "setvalue":
  50. input = this.jquery && this.length > 0 ? this[0] : this;
  51. $(input).val(options);
  52. if (input.inputmask !== undefined) {
  53. $(input).triggerHandler("setvalue.inputmask");
  54. }
  55. break;
  56. case "option":
  57. if (typeof options === "string") {
  58. input = this.jquery && this.length > 0 ? this[0] : this;
  59. if (input.inputmask !== undefined) {
  60. return input.inputmask.option(options);
  61. }
  62. } else {
  63. return this.each(function() {
  64. if (this.inputmask !== undefined) {
  65. return this.inputmask.option(options);
  66. }
  67. });
  68. }
  69. break;
  70. default:
  71. options.alias = fn;
  72. nptmask = new Inputmask(options);
  73. return this.each(function() {
  74. nptmask.mask(this);
  75. });
  76. }
  77. } else if (typeof fn == "object") {
  78. nptmask = new Inputmask(fn);
  79. if (fn.mask === undefined && fn.alias === undefined) {
  80. return this.each(function() {
  81. if (this.inputmask !== undefined) {
  82. return this.inputmask.option(fn);
  83. } else nptmask.mask(this);
  84. });
  85. } else {
  86. return this.each(function() {
  87. nptmask.mask(this);
  88. });
  89. }
  90. } else if (fn === undefined) {
  91. //look for data-inputmask atributes
  92. return this.each(function() {
  93. nptmask = new Inputmask(options);
  94. nptmask.mask(this);
  95. });
  96. }
  97. };
  98. }
  99. return $.fn.inputmask;
  100. }));