plugin_maskedinput.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. jQuery Masked Input Plugin
  3. Copyright (c) 2007 - 2015 Josh Bush (digitalbush.com)
  4. Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license)
  5. Version: 1.4.1
  6. */
  7. !function (factory) {
  8. "function" == typeof define && define.amd ? define(["jquery"], factory) : factory("object" == typeof exports ? require("jquery") : jQuery);
  9. }(function ($) {
  10. var caretTimeoutId, ua = navigator.userAgent, iPhone = /iphone/i.test(ua), chrome = /chrome/i.test(ua), android = /android/i.test(ua);
  11. $.mask = {
  12. definitions: {
  13. "9": "[0-9]",
  14. a: "[A-Za-z]",
  15. "*": "[A-Za-z0-9]"
  16. },
  17. autoclear: !0,
  18. dataName: "rawMaskFn",
  19. placeholder: "_"
  20. }, $.fn.extend({
  21. caret: function (begin, end) {
  22. var range;
  23. if (0 !== this.length && !this.is(":hidden"))
  24. return "number" == typeof begin ? (end = "number" == typeof end ? end : begin,
  25. this.each(function () {
  26. this.setSelectionRange ? this.setSelectionRange(begin, end) : this.createTextRange && (range = this.createTextRange(),
  27. range.collapse(!0), range.moveEnd("character", end), range.moveStart("character", begin),
  28. range.select());
  29. })) : (this[0].setSelectionRange ? (begin = this[0].selectionStart, end = this[0].selectionEnd) : document.selection && document.selection.createRange && (range = document.selection.createRange(),
  30. begin = 0 - range.duplicate().moveStart("character", -1e5), end = begin + range.text.length),
  31. {
  32. begin: begin,
  33. end: end
  34. });
  35. },
  36. unmask: function () {
  37. return this.trigger("unmask");
  38. },
  39. mask: function (mask, settings) {
  40. var input, defs, tests, partialPosition, firstNonMaskPos, lastRequiredNonMaskPos, len, oldVal;
  41. if (!mask && this.length > 0) {
  42. input = $(this[0]);
  43. var fn = input.data($.mask.dataName);
  44. return fn ? fn() : void 0;
  45. }
  46. return settings = $.extend({
  47. autoclear: $.mask.autoclear,
  48. placeholder: $.mask.placeholder,
  49. completed: null
  50. }, settings), defs = $.mask.definitions, tests = [], partialPosition = len = mask.length,
  51. firstNonMaskPos = null, $.each(mask.split(""), function (i, c) {
  52. "?" == c ? (len--, partialPosition = i) : defs[c] ? (tests.push(new RegExp(defs[c])),
  53. null === firstNonMaskPos && (firstNonMaskPos = tests.length - 1), partialPosition > i && (lastRequiredNonMaskPos = tests.length - 1)) : tests.push(null);
  54. }), this.trigger("unmask").each(function () {
  55. function tryFireCompleted() {
  56. if (settings.completed) {
  57. for (var i = firstNonMaskPos; lastRequiredNonMaskPos >= i; i++)
  58. if (tests[i] && buffer[i] === getPlaceholder(i))
  59. return;
  60. settings.completed.call(input);
  61. }
  62. }
  63. function getPlaceholder(i) {
  64. return settings.placeholder.charAt(i < settings.placeholder.length ? i : 0);
  65. }
  66. function seekNext(pos) {
  67. for (; ++pos < len && !tests[pos]; )
  68. ;
  69. return pos;
  70. }
  71. function seekPrev(pos) {
  72. for (; --pos >= 0 && !tests[pos]; )
  73. ;
  74. return pos;
  75. }
  76. function shiftL(begin, end) {
  77. var i, j;
  78. if (!(0 > begin)) {
  79. for (i = begin, j = seekNext(end); len > i; i++)
  80. if (tests[i]) {
  81. if (!(len > j && tests[i].test(buffer[j])))
  82. break;
  83. buffer[i] = buffer[j], buffer[j] = getPlaceholder(j), j = seekNext(j);
  84. }
  85. writeBuffer(), input.caret(Math.max(firstNonMaskPos, begin));
  86. }
  87. }
  88. function shiftR(pos) {
  89. var i, c, j, t;
  90. for (i = pos, c = getPlaceholder(pos); len > i; i++)
  91. if (tests[i]) {
  92. if (j = seekNext(i), t = buffer[i], buffer[i] = c, !(len > j && tests[j].test(t)))
  93. break;
  94. c = t;
  95. }
  96. }
  97. function androidInputEvent() {
  98. var curVal = input.val(), pos = input.caret();
  99. if (oldVal && oldVal.length && oldVal.length > curVal.length) {
  100. for (checkVal(!0); pos.begin > 0 && !tests[pos.begin - 1]; )
  101. pos.begin--;
  102. if (0 === pos.begin)
  103. for (; pos.begin < firstNonMaskPos && !tests[pos.begin]; )
  104. pos.begin++;
  105. input.caret(pos.begin, pos.begin);
  106. } else {
  107. for (checkVal(!0); pos.begin < len && !tests[pos.begin]; )
  108. pos.begin++;
  109. input.caret(pos.begin, pos.begin);
  110. }
  111. tryFireCompleted();
  112. }
  113. function blurEvent() {
  114. checkVal(), input.val() != focusText && input.change();
  115. }
  116. function keydownEvent(e) {
  117. if (!input.prop("readonly")) {
  118. var pos, begin, end, k = e.which || e.keyCode;
  119. oldVal = input.val(), 8 === k || 46 === k || iPhone && 127 === k ? (pos = input.caret(),
  120. begin = pos.begin, end = pos.end, end - begin === 0 && (begin = 46 !== k ? seekPrev(begin) : end = seekNext(begin - 1),
  121. end = 46 === k ? seekNext(end) : end), clearBuffer(begin, end), shiftL(begin, end - 1),
  122. e.preventDefault()) : 13 === k ? blurEvent.call(this, e) : 27 === k && (input.val(focusText),
  123. input.caret(0, checkVal()), e.preventDefault());
  124. }
  125. }
  126. function keypressEvent(e) {
  127. if (!input.prop("readonly")) {
  128. var p, c, next, k = e.which || e.keyCode, pos = input.caret();
  129. if (!(e.ctrlKey || e.altKey || e.metaKey || 32 > k) && k && 13 !== k) {
  130. if (pos.end - pos.begin !== 0 && (clearBuffer(pos.begin, pos.end), shiftL(pos.begin, pos.end - 1)),
  131. p = seekNext(pos.begin - 1), len > p && (c = String.fromCharCode(k), tests[p].test(c))) {
  132. if (shiftR(p), buffer[p] = c, writeBuffer(), next = seekNext(p), android) {
  133. var proxy = function () {
  134. $.proxy($.fn.caret, input, next)();
  135. };
  136. setTimeout(proxy, 0);
  137. } else
  138. input.caret(next);
  139. pos.begin <= lastRequiredNonMaskPos && tryFireCompleted();
  140. }
  141. e.preventDefault();
  142. }
  143. }
  144. }
  145. function clearBuffer(start, end) {
  146. var i;
  147. for (i = start; end > i && len > i; i++)
  148. tests[i] && (buffer[i] = getPlaceholder(i));
  149. }
  150. function writeBuffer() {
  151. input.val(buffer.join(""));
  152. }
  153. function checkVal(allow) {
  154. var i, c, pos, test = input.val(), lastMatch = -1;
  155. for (i = 0, pos = 0; len > i; i++)
  156. if (tests[i]) {
  157. for (buffer[i] = getPlaceholder(i); pos++ < test.length; )
  158. if (c = test.charAt(pos - 1),
  159. tests[i].test(c)) {
  160. buffer[i] = c, lastMatch = i;
  161. break;
  162. }
  163. if (pos > test.length) {
  164. clearBuffer(i + 1, len);
  165. break;
  166. }
  167. } else
  168. buffer[i] === test.charAt(pos) && pos++, partialPosition > i && (lastMatch = i);
  169. return allow ? writeBuffer() : partialPosition > lastMatch + 1 ? settings.autoclear || buffer.join("") === defaultBuffer ? (input.val() && input.val(""),
  170. clearBuffer(0, len)) : writeBuffer() : (writeBuffer(), input.val(input.val().substring(0, lastMatch + 1))),
  171. partialPosition ? i : firstNonMaskPos;
  172. }
  173. var input = $(this), buffer = $.map(mask.split(""), function (c, i) {
  174. return "?" != c ? defs[c] ? getPlaceholder(i) : c : void 0;
  175. }), defaultBuffer = buffer.join(""), focusText = input.val();
  176. input.data($.mask.dataName, function () {
  177. return $.map(buffer, function (c, i) {
  178. return tests[i] && c != getPlaceholder(i) ? c : null;
  179. }).join("");
  180. }), input.one("unmask", function () {
  181. input.off(".mask").removeData($.mask.dataName);
  182. }).on("focus.mask", function () {
  183. if (!input.prop("readonly")) {
  184. clearTimeout(caretTimeoutId);
  185. var pos;
  186. focusText = input.val(), pos = checkVal(), caretTimeoutId = setTimeout(function () {
  187. input.get(0) === document.activeElement && (writeBuffer(), pos == mask.replace("?", "").length ? input.caret(0, pos) : input.caret(pos));
  188. }, 10);
  189. }
  190. }).on("blur.mask", blurEvent).on("keydown.mask", keydownEvent).on("keypress.mask", keypressEvent).on("input.mask paste.mask", function () {
  191. input.prop("readonly") || setTimeout(function () {
  192. var pos = checkVal(!0);
  193. input.caret(pos), tryFireCompleted();
  194. }, 0);
  195. }), chrome && android && input.off("input.mask").on("input.mask", androidInputEvent),
  196. checkVal();
  197. });
  198. }
  199. });
  200. });