/* * @license Multi Input Mask plugin for jquery * https://github.com/andr-04/inputmask-multi * Copyright (c) 2012 Andrey Egorov * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php) * Version: 1.0.2 * * Requriements: * https://github.com/RobinHerbots/jquery.inputmask * https://github.com/private-face/jquery.bind-first */ (function ($) { $.masksLoad = function(url) { var maskList; $.ajax({ url: url, async: false, dataType: 'json', success: function (response) { maskList = response; } }); return maskList; } $.masksSort = function(maskList, defs, match, key) { maskList.sort(function (a, b) { var ia = 0, ib = 0; for (; (ia 0 || newMask.charAt(pos) != maskOpts.replace)); pos++) { if (newMask.charAt(pos) == maskOpts.replace) { startPos--; } } startPos = pos; for (; (pos < newMask.length && endPos > 0); pos++) { if (newMask.charAt(pos) == maskOpts.replace) { endPos--; } } endPos = pos; return { begin: startPos, end: endPos }; } var maskUnbind = function() { $(this) .unbind("keypress.inputmask", masksKeyPress) .unbind("input.inputmask", masksPaste) .unbind("paste.inputmask", masksPaste) .unbind("dragdrop.inputmask", masksPaste) .unbind("drop.inputmask", masksPaste) .unbind("keydown.inputmask", masksKeyDown) .unbind("setvalue.inputmask", masksSetValue) .unbind("blur.inputmask", masksChange); } var maskRebind = function() { maskUnbind.call(this); $(this) .bindFirst("keypress.inputmask", masksKeyPress) .bindFirst("input.inputmask", masksPaste) .bindFirst("paste.inputmask", masksPaste) .bindFirst("dragdrop.inputmask", masksPaste) .bindFirst("drop.inputmask", masksPaste) .bindFirst("keydown.inputmask", masksKeyDown) .bindFirst("setvalue.inputmask", masksSetValue) .bind("blur.inputmask", masksChange); } var maskApply = function(match, newtext) { if (match && (newtext || match.mask != oldmatch.mask)) { var caretPos; if (!newtext) { caretPos = caretApply(oldmatch.mask, match.mask, caret.call(this)); } if (newtext) { if (this._valueSet) { this._valueSet(newtext); } else { this.value = newtext; } } $(this).inputmask(match.mask, $.extend(true, maskOpts.inputmask, { insertMode: insertMode })); if (!newtext) { caret.call(this, caretPos.begin, caretPos.end); } } oldmatch = match; maskOpts.onMaskChange.call(this, match.obj, match.determined); return true; } var keyboardApply = function(e, text, insert) { var match = maskMatch(text); if (!match || match.obj != oldmatch.obj || match.determined != oldmatch.determined) { if (match) { maskUnbind.call(this); if (insert) { maskApply.call(this, match); $(this).trigger(e); } else { $(this).trigger(e); maskApply.call(this, match); } maskRebind.call(this); } e.stopImmediatePropagation(); return false; } return true; } var masksKeyDown = function(e) { e = e || window.event; var k = e.which || e.charCode || e.keyCode; if (k == 8 || k == 46 || (iphone && k == 127)) { // delete or backspace var text = this._valueGet(); var caretPos = caret.call(this); if (caretPos.begin == caretPos.end || (!insertMode && caretPos.begin == caretPos.end-1)) { var pos = caretPos.begin; do { if (k != 46) { // backspace pos--; } var chr = text.charAt(pos); text = text.substring(0, pos) + text.substring(pos+1); } while (pos>0 && pos0) { text = text.substr(0, text.length-1); match = maskMatch(text); } maskApply.call(this, match, text); maskRebind.call(this); } var masksPaste = function(e) { var input = this; setTimeout(function() { maskInit.call(input); }, 0); e.stopImmediatePropagation(); return true; } switch (mode) { case "isCompleted": var res = maskMatch((this[0]._valueGet && this[0]._valueGet()) || this[0].value); return (res && res.completed); default: this.each(function () { maskInit.call(this); }); return this; } } })(jQuery);