jquery.inputmask.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. $.inputmask = inputmask.prototype; //needed for legacy until all is reverted to the inputmask object
  12. $.fn.inputmask = function(fn, options) {
  13. var nptmask;
  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 = options || {};
  42. options.alias = fn;
  43. nptmask = new inputmask(options);
  44. return this.each(function() {
  45. nptmask.mask(this);
  46. });
  47. }
  48. } else if (typeof fn == "object") {
  49. nptmask = new inputmask(fn);
  50. return this.each(function() {
  51. nptmask.mask(this);
  52. });
  53. } else if (fn == undefined) {
  54. //look for data-inputmask atributes
  55. return this.each(function() {
  56. nptmask = new inputmask();
  57. nptmask.mask(this);
  58. });
  59. }
  60. };
  61. }
  62. return $.fn.inputmask;
  63. })(jQuery);