jquery.inputmask.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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($) {
  9. if ($.fn.inputmask === undefined) {
  10. //jquery plugin
  11. $.fn.inputmask = function(fn, options) {
  12. var nptmask;
  13. options = options || {};
  14. if (typeof fn === "string") {
  15. switch (fn) {
  16. case "mask":
  17. nptmask = new inputmask(options);
  18. return this.each(function() {
  19. nptmask.mask(this);
  20. });
  21. case "unmaskedvalue":
  22. var input = this.jquery && this.length > 0 ? this[0] : this;
  23. return input.inputmask ? input.inputmask.unmaskedvalue() : $(input).val();
  24. case "remove":
  25. return this.each(function() {
  26. if (this.inputmask) this.inputmask.remove();
  27. });
  28. case "getemptymask":
  29. var input = this.jquery && this.length > 0 ? this[0] : this;
  30. return input.inputmask ? input.inputmask.getemptymask() : "";
  31. case "hasMaskedValue": //check wheter the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value
  32. var input = this.jquery && this.length > 0 ? this[0] : this;
  33. return input.inputmask ? input.inputmask.hasMaskedValue() : false;
  34. case "isComplete":
  35. var input = this.jquery && this.length > 0 ? this[0] : this;
  36. return input.inputmask ? input.inputmask.isComplete() : true;
  37. case "getmetadata": //return mask metadata if exists
  38. var input = this.jquery && this.length > 0 ? this[0] : this;
  39. return input.inputmask ? input.inputmask.getmetadata() : undefined;
  40. default:
  41. options.alias = fn;
  42. nptmask = new inputmask(options);
  43. return this.each(function() {
  44. nptmask.mask(this);
  45. });
  46. }
  47. } else if (typeof fn == "object") {
  48. nptmask = new inputmask(fn);
  49. return this.each(function() {
  50. nptmask.mask(this);
  51. });
  52. } else if (fn == undefined) {
  53. //look for data-inputmask atributes
  54. return this.each(function() {
  55. nptmask = new inputmask(options);
  56. nptmask.mask(this);
  57. });
  58. }
  59. };
  60. }
  61. return $.fn.inputmask;
  62. })(jQuery);