jquery.inputmask.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 = this[0];
  22. if (options === undefined) options = {};
  23. if (typeof fn === "string") {
  24. switch (fn) {
  25. case "unmaskedvalue":
  26. return input && input.inputmask ? input.inputmask.unmaskedvalue() : $(input).val();
  27. case "remove":
  28. return this.each(function () {
  29. if (this.inputmask) this.inputmask.remove();
  30. });
  31. case "getemptymask":
  32. return input && input.inputmask ? input.inputmask.getemptymask() : "";
  33. case "hasMaskedValue": //check whether the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value
  34. return input && input.inputmask ? input.inputmask.hasMaskedValue() : false;
  35. case "isComplete":
  36. return input && input.inputmask ? input.inputmask.isComplete() : true;
  37. case "getmetadata": //return mask metadata if exists
  38. return input && input.inputmask ? input.inputmask.getmetadata() : undefined;
  39. case "setvalue":
  40. Inputmask.setValue(input, options);
  41. break;
  42. case "option":
  43. if (typeof options === "string") {
  44. if (input && input.inputmask !== undefined) {
  45. return input.inputmask.option(options);
  46. }
  47. } else {
  48. return this.each(function () {
  49. if (this.inputmask !== undefined) {
  50. return this.inputmask.option(options);
  51. }
  52. });
  53. }
  54. break;
  55. default:
  56. options.alias = fn;
  57. nptmask = new Inputmask(options);
  58. return this.each(function () {
  59. nptmask.mask(this);
  60. });
  61. }
  62. } else if (Array.isArray(fn)) {
  63. options.alias = fn;
  64. nptmask = new Inputmask(options);
  65. return this.each(function () {
  66. nptmask.mask(this);
  67. });
  68. } else if (typeof fn == "object") {
  69. nptmask = new Inputmask(fn);
  70. if (fn.mask === undefined && fn.alias === undefined) {
  71. return this.each(function () {
  72. if (this.inputmask !== undefined) {
  73. return this.inputmask.option(fn);
  74. } else nptmask.mask(this);
  75. });
  76. } else {
  77. return this.each(function () {
  78. nptmask.mask(this);
  79. });
  80. }
  81. } else if (fn === undefined) {
  82. //look for data-inputmask atributes
  83. return this.each(function () {
  84. nptmask = new Inputmask(options);
  85. nptmask.mask(this);
  86. });
  87. }
  88. };
  89. }
  90. return $.fn.inputmask;
  91. }));