jquery.inputmask.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Input Mask plugin for jquery
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) Robin Herbots
  5. * Licensed under the MIT license
  6. */
  7. import $ from "jquery";
  8. import Inputmask from "./inputmask";
  9. if ($.fn.inputmask === undefined) {
  10. //jquery plugin
  11. $.fn.inputmask = function (fn, options) {
  12. var nptmask, input = this[0];
  13. if (options === undefined) options = {};
  14. if (typeof fn === "string") {
  15. switch (fn) {
  16. case "unmaskedvalue":
  17. return input && input.inputmask ? input.inputmask.unmaskedvalue() : $(input).val();
  18. case "remove":
  19. return this.each(function () {
  20. if (this.inputmask) this.inputmask.remove();
  21. });
  22. case "getemptymask":
  23. return input && input.inputmask ? input.inputmask.getemptymask() : "";
  24. case "hasMaskedValue": //check whether the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value
  25. return input && input.inputmask ? input.inputmask.hasMaskedValue() : false;
  26. case "isComplete":
  27. return input && input.inputmask ? input.inputmask.isComplete() : true;
  28. case "getmetadata": //return mask metadata if exists
  29. return input && input.inputmask ? input.inputmask.getmetadata() : undefined;
  30. case "setvalue":
  31. Inputmask.setValue(input, options);
  32. break;
  33. case "option":
  34. if (typeof options === "string") {
  35. if (input && input.inputmask !== undefined) {
  36. return input.inputmask.option(options);
  37. }
  38. } else {
  39. return this.each(function () {
  40. if (this.inputmask !== undefined) {
  41. return this.inputmask.option(options);
  42. }
  43. });
  44. }
  45. break;
  46. default:
  47. options.alias = fn;
  48. nptmask = new Inputmask(options);
  49. return this.each(function () {
  50. nptmask.mask(this);
  51. });
  52. }
  53. } else if (Array.isArray(fn)) {
  54. options.alias = fn;
  55. nptmask = new Inputmask(options);
  56. return this.each(function () {
  57. nptmask.mask(this);
  58. });
  59. } else if (typeof fn == "object") {
  60. nptmask = new Inputmask(fn);
  61. if (fn.mask === undefined && fn.alias === undefined) {
  62. return this.each(function () {
  63. if (this.inputmask !== undefined) {
  64. return this.inputmask.option(fn);
  65. } else nptmask.mask(this);
  66. });
  67. } else {
  68. return this.each(function () {
  69. nptmask.mask(this);
  70. });
  71. }
  72. } else if (fn === undefined) {
  73. //look for data-inputmask atributes
  74. return this.each(function () {
  75. nptmask = new Inputmask(options);
  76. nptmask.mask(this);
  77. });
  78. }
  79. };
  80. }