jquery.inputmask.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 wheter 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. $(input).val(options);
  41. if (input && input.inputmask === undefined) { //reactivate jquery.clone
  42. $(input).triggerHandler("setvalue");
  43. }
  44. break;
  45. case "option":
  46. if (typeof options === "string") {
  47. if (input && input.inputmask !== undefined) {
  48. return input.inputmask.option(options);
  49. }
  50. } else {
  51. return this.each(function () {
  52. if (this.inputmask !== undefined) {
  53. return this.inputmask.option(options);
  54. }
  55. });
  56. }
  57. break;
  58. default:
  59. options.alias = fn;
  60. nptmask = new Inputmask(options);
  61. return this.each(function () {
  62. nptmask.mask(this);
  63. });
  64. }
  65. } else if (typeof fn == "object") {
  66. nptmask = new Inputmask(fn);
  67. if (fn.mask === undefined && fn.alias === undefined) {
  68. return this.each(function () {
  69. if (this.inputmask !== undefined) {
  70. return this.inputmask.option(fn);
  71. } else nptmask.mask(this);
  72. });
  73. } else {
  74. return this.each(function () {
  75. nptmask.mask(this);
  76. });
  77. }
  78. } else if (fn === undefined) {
  79. //look for data-inputmask atributes
  80. return this.each(function () {
  81. nptmask = new Inputmask(options);
  82. nptmask.mask(this);
  83. });
  84. }
  85. };
  86. }
  87. return $.fn.inputmask;
  88. }));