/** * @license Input Mask plugin for jquery * http://github.com/RobinHerbots/jquery.inputmask * Copyright (c) 2010 - 2014 Robin Herbots * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php) * Version: 0.0.0 */ (function ($) { if ($.fn.inputmask === undefined) { //helper functions function isInputEventSupported(eventName) { var el = document.createElement('input'), eventName = 'on' + eventName, isSupported = (eventName in el); if (!isSupported) { el.setAttribute(eventName, 'return;'); isSupported = typeof el[eventName] == 'function'; } el = null; return isSupported; } function resolveAlias(aliasStr, options, opts) { var aliasDefinition = opts.aliases[aliasStr]; if (aliasDefinition) { if (aliasDefinition.alias) resolveAlias(aliasDefinition.alias, undefined, opts); //alias is another alias $.extend(true, opts, aliasDefinition); //merge alias definition in the options $.extend(true, opts, options); //reapply extra given options return true; } return false; } function generateMaskSet(opts) { var ms; function analyseMask(mask) { if (opts.numericInput) { mask = mask.split('').reverse().join(''); } var tokenizer = /(?:[?*+]|\{[0-9]+(?:,[0-9]*)?\})\??|[^.?*+^${[]()|\\]+|./g, escaped = false; function maskToken(isGroup, isOptional, isQuantifier) { this.matches = []; this.isGroup = isGroup || false; this.isOptional = isOptional || false; this.isQuantifier = isQuantifier || false; this.quantifier = { min: 1, max: 1 }; }; //test definition => {fn: RegExp/function, cardinality: int, optionality: bool, newBlockMarker: bool, offset: int, casing: null/upper/lower, def: definitionSymbol} function insertTestDefinition(mtoken, element, position) { var maskdef = opts.definitions[element]; position = position != undefined ? position : mtoken.matches.length; if (maskdef && !escaped) { var prevalidators = maskdef["prevalidator"], prevalidatorsL = prevalidators ? prevalidators.length : 0; for (var i = 1; i < maskdef.cardinality; i++) { var prevalidator = prevalidatorsL >= i ? prevalidators[i - 1] : [], validator = prevalidator["validator"], cardinality = prevalidator["cardinality"]; mtoken.matches.splice(position++, 0, { fn: validator ? typeof validator == 'string' ? new RegExp(validator) : new function () { this.test = validator; } : new RegExp("."), cardinality: cardinality ? cardinality : 1, optionality: mtoken.isOptional, casing: maskdef["casing"], def: maskdef["definitionSymbol"] || element }); } mtoken.matches.splice(position++, 0, { fn: maskdef.validator ? typeof maskdef.validator == 'string' ? new RegExp(maskdef.validator) : new function () { this.test = maskdef.validator; } : new RegExp("."), cardinality: maskdef.cardinality, optionality: mtoken.isOptional, casing: maskdef["casing"], def: maskdef["definitionSymbol"] || element }); } else { mtoken.matches.splice(position++, 0, { fn: null, cardinality: 0, optionality: mtoken.isOptional, casing: null, def: element }); escaped = false; } } var currentToken = new maskToken(), match, m, openenings = [], maskTokens = []; while (match = tokenizer.exec(mask)) { m = match[0]; switch (m.charAt(0)) { case opts.optionalmarker.end: // optional closing case opts.groupmarker.end: // Group closing var openingToken = openenings.pop(); if (openenings.length > 0) { openenings[openenings.length - 1]["matches"].push(openingToken); } else { currentToken.matches.push(openingToken); } break; case opts.optionalmarker.start: // optional opening openenings.push(new maskToken(false, true)); break; case opts.groupmarker.start: // Group opening openenings.push(new maskToken(true)); break; case opts.quantifiermarker.start: //Quantifier var quantifier = new maskToken(false, false, true); m = m.replace(/[{}]/g, ""); var mq = m.split(","), mq0 = isNaN(mq[0]) ? mq[0] : parseInt(mq[0]), mq1 = mq.length == 1 ? mq0 : (isNaN(mq[1]) ? mq[1] : parseInt(mq[1])); quantifier.quantifier = { min: mq0, max: mq1 }; if (openenings.length > 0) { var matches = openenings[openenings.length - 1]["matches"]; var match = matches.pop(); if (!match["isGroup"]) { var groupToken = new maskToken(true); groupToken.matches.push(match); match = groupToken; } matches.push(match); matches.push(quantifier); } else { var match = currentToken.matches.pop(); if (!match["isGroup"]) { var groupToken = new maskToken(true); groupToken.matches.push(match); match = groupToken; } currentToken.matches.push(match); currentToken.matches.push(quantifier); } break; case opts.escapeChar: escaped = true; break; default: if (openenings.length > 0) { insertTestDefinition(openenings[openenings.length - 1], m); } else { if (currentToken.matches.length > 0) { var lastMatch = currentToken.matches[currentToken.matches.length - 1]; if (lastMatch["isGroup"]) { //this is not a group but a normal mask => convert lastMatch.isGroup = false; insertTestDefinition(lastMatch, opts.groupmarker.start, 0); insertTestDefinition(lastMatch, opts.groupmarker.end); } } insertTestDefinition(currentToken, m); } } } if (currentToken.matches.length > 0) maskTokens.push(currentToken); if (opts.repeat > 0 || opts.repeat == "*" || opts.repeat == "+") { var groupedMaskToken = new maskToken(false, false, false); var groupToken = new maskToken(true), quantifierToken = new maskToken(false, false, true); quantifierToken.quantifier = opts.repeat == "*" ? { min: 0, max: "*" } : (opts.repeat == "+" ? { min: 1, max: "*" } : { min: opts.greedy ? opts.repeat : 1, max: opts.repeat }); if (maskTokens.length > 1) { groupToken.matches = maskTokens; groupedMaskToken.matches.push(groupToken); groupedMaskToken.matches.push(quantifierToken); } else { groupToken.matches = maskTokens[0].matches; groupedMaskToken.matches.push(groupToken); groupedMaskToken.matches.push(quantifierToken); } maskTokens = [groupedMaskToken]; } console.log(JSON.stringify(maskTokens)); return maskTokens; } function generateMask(mask, metadata) { return mask == undefined || mask == "" ? undefined : { "mask": mask, "maskToken": analyseMask(mask), "validPositions": {}, "_buffer": undefined, "buffer": undefined, "tests": {}, "metadata": metadata }; } if (opts.repeat == "*" || opts.repeat == "+") opts.greedy = false; if ($.isFunction(opts.mask)) { //allow mask to be a preprocessing fn - should return a valid mask opts.mask = opts.mask.call(this, opts); } if ($.isArray(opts.mask)) { $.each(opts.mask, function (ndx, msk) { if (msk["mask"] != undefined) { ms = generateMask(msk["mask"].toString(), msk); } else { ms = generateMask(msk.toString()); } return false; //break after first multiple not supported for now (again) }); } else { if (opts.mask.length == 1 && opts.greedy == false && opts.repeat != 0) { opts.placeholder = ""; } //hide placeholder with single non-greedy mask if (opts.mask["mask"] != undefined) { ms = generateMask(opts.mask["mask"].toString(), opts.mask); } else { ms = generateMask(opts.mask.toString()); } } return ms; } var msie1x = typeof ScriptEngineMajorVersion === "function" ? ScriptEngineMajorVersion() //IE11 detection : new Function("/*@cc_on return @_jscript_version; @*/")() >= 10, //conditional compilation from mickeysoft trick ua = navigator.userAgent, iphone = ua.match(new RegExp("iphone", "i")) !== null, android = ua.match(new RegExp("android.*safari.*", "i")) !== null, androidchrome = ua.match(new RegExp("android.*chrome.*", "i")) !== null, androidfirefox = ua.match(new RegExp("android.*firefox.*", "i")) !== null, kindle = /Kindle/i.test(ua) || /Silk/i.test(ua) || /KFTT/i.test(ua) || /KFOT/i.test(ua) || /KFJWA/i.test(ua) || /KFJWI/i.test(ua) || /KFSOWI/i.test(ua) || /KFTHWA/i.test(ua) || /KFTHWI/i.test(ua) || /KFAPWA/i.test(ua) || /KFAPWI/i.test(ua), PasteEventType = isInputEventSupported('paste') ? 'paste' : isInputEventSupported('input') ? 'input' : "propertychange"; //if (androidchrome) { // var browser = navigator.userAgent.match(new RegExp("chrome.*", "i")), // version = parseInt(new RegExp(/[0-9]+/).exec(browser)); // androidchrome32 = (version == 32); //} //masking scope //actionObj definition see below function maskScope(maskset, opts, actionObj) { var isRTL = false, valueOnFocus = getBuffer().join(''), $el, skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround skipInputEvent = false, //skip when triggered from within inputmask ignorable = false; //maskset helperfunctions function getMaskTemplate(baseOnInput, minimalPos) { minimalPos = minimalPos || 0; var maskTemplate = [], ndxIntlzr, pos = 0, test; do { if (baseOnInput === true && getMaskSet()['validPositions'][pos]) { var validPos = getMaskSet()['validPositions'][pos]; test = validPos["match"]; ndxIntlzr = validPos["locator"].slice(); maskTemplate.push(test["fn"] == null ? test["def"] : opts.placeholder.charAt(pos % opts.placeholder.length)); } else { var testPos = getTests(pos, false, ndxIntlzr, pos - 1); testPos = testPos[opts.greedy || minimalPos > pos ? 0 : (testPos.length - 1)]; test = testPos["match"]; ndxIntlzr = testPos["locator"].slice(); maskTemplate.push(test["fn"] == null ? test["def"] : opts.placeholder.charAt(pos % opts.placeholder.length)); } pos++; } while (test["fn"] != null || (test["fn"] == null && test["def"] != "") || minimalPos >= pos); maskTemplate.pop(); //drop the last one which is empty return maskTemplate; } function getMaskSet() { return maskset; } function resetMaskSet() { var maskset = getMaskSet(); maskset["buffer"] = undefined; maskset["_buffer"] = undefined; maskset["validPositions"] = {}; maskset["p"] = -1; } function getLastValidPosition(maskset, closestTo) { //TODO implement closest to maskset = maskset || getMaskSet(); var lastValidPosition = -1; for (var posNdx in maskset["validPositions"]) { var psNdx = parseInt(posNdx); if (psNdx > lastValidPosition) lastValidPosition = psNdx; } return lastValidPosition; } function setLastValidPosition(pos, maskset) { maskset = maskset || getMaskSet(); for (var posNdx in maskset["validPositions"]) { if (posNdx > pos) maskset["validPositions"][posNdx] = undefined; } } function getTest(pos) { if (getMaskSet()['validPositions'][pos]) { return getMaskSet()['validPositions'][pos]["match"]; } return getTests(pos)[0]["match"]; } function getTests(pos, disableCache, ndxIntlzr, tstPs) { var maskTokens = getMaskSet()["maskToken"], testPos = ndxIntlzr ? tstPs : 0, ndxInitializer = ndxIntlzr || [0], matches = [], insertStop = false; function ResolveTestFromToken(maskToken, ndxInitializer, loopNdx, quantifierRecurse) { //ndxInitilizer contains a set of indexes to speedup searches in the mtokens function handleMatch(match, loopNdx, quantifierRecurse) { var currentPos = testPos; if (testPos == pos && match.matches == undefined) { matches.push({ "match": match, "locator": loopNdx.reverse() }); return true; } else if (match.matches != undefined) { if (match.isGroup && quantifierRecurse !== true) { //when a group pass along to the quantifier match = handleMatch(maskToken.matches[tndx + 1], loopNdx); if (match) return true; } else if (match.isOptional) { match = ResolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse); if (match) { //search for next possible match testPos = currentPos; } } else if (match.isQuantifier && quantifierRecurse !== true) { var qt = match; for (var qndx = (ndxInitializer.length > 0 && quantifierRecurse !== true) ? ndxInitializer.shift() : 0; (qndx < (isNaN(qt.quantifier.max) ? qndx + 1 : qt.quantifier.max)) && testPos <= pos; qndx++) { var tokenGroup = maskToken.matches[maskToken.matches.indexOf(qt) - 1]; match = handleMatch(tokenGroup, [qndx].concat(loopNdx), true); if (match) { //get latest match var latestMatch = matches[matches.length - 1]["match"]; var isFirstMatch = (tokenGroup.matches.indexOf(latestMatch) == 0); if (isFirstMatch) { //search for next possible match if (qndx > qt.quantifier.min - 1) { insertStop = true; testPos = pos; //match the position after the group break; //stop quantifierloop } else return true; } else { return true; } } } } else { match = ResolveTestFromToken(match, ndxInitializer, loopNdx, quantifierRecurse); if (match) return true; } } else testPos++; } for (var tndx = (ndxInitializer.length > 0 ? ndxInitializer.shift() : 0) ; tndx < maskToken.matches.length; tndx++) { if (maskToken.matches[tndx]["isQuantifier"] !== true) { var match = handleMatch(maskToken.matches[tndx], [tndx].concat(loopNdx), quantifierRecurse); if (match && testPos == pos) { return match; } else if (testPos > pos) { break; } } } } if (disableCache !== true && getMaskSet()['tests'][pos] && !getMaskSet()['validPositions'][pos]) { return getMaskSet()['tests'][pos]; } if (ndxIntlzr == undefined) { var previousPos = pos - 1, test; while ((test = getMaskSet()['validPositions'][previousPos]) == undefined && previousPos > -1) { previousPos--; } if (test != undefined && previousPos > -1) { testPos = previousPos; ndxInitializer = test["locator"].slice(); } else { previousPos = pos - 1; while ((test = getMaskSet()['tests'][previousPos]) == undefined && previousPos > -1) { previousPos--; } if (test != undefined && previousPos > -1) { testPos = previousPos; ndxInitializer = test[0]["locator"].slice(); } } } for (var mtndx = ndxInitializer.shift() ; mtndx < maskTokens.length; mtndx++) { var match = ResolveTestFromToken(maskTokens[mtndx], ndxInitializer, [mtndx]); if ((match && testPos == pos) || testPos > pos) { break; } } if (matches.length == 0 || insertStop) matches.push({ "match": { fn: null, cardinality: 0, optionality: true, casing: null, def: "" }, "locator": [] }); getMaskSet()['tests'][pos] = matches; console.log(pos + " - " + JSON.stringify(matches)); return matches; } function getBufferTemplate() { if (getMaskSet()['_buffer'] == undefined) { //generate template getMaskSet()["_buffer"] = getMaskTemplate(false, 1); } return getMaskSet()['_buffer']; } function getBuffer() { if (getMaskSet()['buffer'] == undefined) { getMaskSet()['buffer'] = getMaskTemplate(true); } return getMaskSet()['buffer']; } function isValid(pos, c, strict) { //strict true ~ no correction or autofill strict = strict === true; //always set a value to strict to prevent possible strange behavior in the extensions function _isValid(position, c, strict) { var rslt = false; $.each(getTests(position, !strict), function (ndx, tst) { var test = tst["match"]; var loopend = c ? 1 : 0, chrs = '', buffer = getBuffer(); for (var i = test.cardinality; i > loopend; i--) { chrs += getBufferElement(buffer, position - (i - 1), true); } if (c) { chrs += c; } //return is false or a json object => { pos: ??, c: ??} or true rslt = test.fn != null ? test.fn.test(chrs, buffer, position, strict, opts) : (c == getPlaceholder(position) || c == opts.skipOptionalPartCharacter) ? { "refresh": true, c: getPlaceholder(position), pos: position } : false; if (rslt !== false) { var elem = c; switch (test.casing) { case "upper": elem = elem.toUpperCase(); break; case "lower": elem = elem.toLowerCase(); break; } var validatedPos = position; if (rslt !== true && rslt["pos"] != position) { validatedPos = rslt["pos"]; tst = getTests(validatedPos, !strict)[0]; //possible mismatch TODO } getMaskSet()["validPositions"][validatedPos] = $.extend({}, tst, { "input": elem }); return false; //break from $.each } }); return rslt; } var maskPos = pos; var result = _isValid(maskPos, c, strict); if (!strict && result === false && !isMask(maskPos)) { //does the input match on a further position? for (var nPos = maskPos + 1, snPos = seekNext(maskPos) ; nPos <= snPos; nPos++) { result = _isValid(nPos, c, strict); if (result !== false) { maskPos = nPos; break; } } } if (result === true) result = { "pos": maskPos }; return result; } function isMask(pos) { var test = getTest(pos); return test.fn != null ? test.fn : false; } function getMaskLength() { var maxLength = $el.prop('maxLength'), maskLength; if (opts.greedy == false) { var lvp = getLastValidPosition() + 1, test = getTest(lvp); while (test.fn != null && test.def != "") { var tests = getTests(++lvp); test = tests[tests.length - 1]; } maskLength = getMaskTemplate(false, lvp).length; } else maskLength = getBuffer().length; return maxLength == undefined || (maskLength < maxLength && maxLength > -1) /* FF sets no defined max length to -1 */ ? maskLength : maxLength; } function seekNext(pos) { var maskL = getMaskLength(); if (pos >= maskL) return maskL; var position = pos; while (++position < maskL && !isMask(position)) { } return position; } function seekPrevious(pos) { var position = pos; if (position <= 0) return 0; while (--position > 0 && !isMask(position)) { } ; return position; } function setBufferElement(buffer, position, element) { position = prepareBuffer(buffer, position); var test = getTest(position); var elem = element; if (elem != undefined && test != undefined) { switch (test.casing) { case "upper": elem = element.toUpperCase(); break; case "lower": elem = element.toLowerCase(); break; } } buffer[position] = elem; } function getBufferElement(buffer, position) { position = prepareBuffer(buffer, position); return buffer[position]; } //needed to handle the non-greedy mask repetitions function prepareBuffer(buffer, position) { //TODO DROP BUFFER PASSING + optimize me if (buffer.length <= position) { var trbuffer = getMaskTemplate(true, position); buffer.length = trbuffer.length; for (var i = 0, bl = buffer.length; i < bl; i++) { if (buffer[i] == undefined) buffer[i] = trbuffer[i]; } buffer[position] = getPlaceholder(position); } return position; } function writeBuffer(input, buffer, caretPos) { input._valueSet(buffer.join('')); if (caretPos != undefined) { caret(input, caretPos); } } function clearBuffer(buffer, start, end, stripNomasks) { for (var i = start, maskL = getMaskLength() ; i < end && i < maskL; i++) { if (stripNomasks === true) { if (!isMask(i)) setBufferElement(buffer, i, ""); } else setBufferElement(buffer, i, getPlaceholder(i)); } } function setPlaceholder(pos) { setBufferElement(getBuffer(), pos, getPlaceholder(pos)); } function getPlaceholder(pos) { var test = getTest(pos); return test["fn"] == null ? test["def"] : opts.placeholder.charAt(pos % opts.placeholder.length); } function checkVal(input, writeOut, strict, nptvl, intelliCheck) { var inputValue = nptvl != undefined ? nptvl.slice() : truncateInput(input._valueGet()).split(''); resetMaskSet(); if (writeOut) input._valueSet(""); //initial clear $.each(inputValue, function (ndx, charCode) { if (intelliCheck === true) { var p = getMaskSet()["p"], lvp = p == -1 ? p : seekPrevious(p), pos = lvp == -1 ? ndx : seekNext(lvp); if ($.inArray(charCode, getBufferTemplate().slice(lvp + 1, pos)) == -1) { keypressEvent.call(input, undefined, true, charCode.charCodeAt(0), writeOut, strict, ndx); } } else { keypressEvent.call(input, undefined, true, charCode.charCodeAt(0), writeOut, strict, ndx); strict = strict || (ndx > 0 && ndx > getMaskSet()["p"]); } }); } function escapeRegex(str) { return $.inputmask.escapeRegex.call(this, str); } function truncateInput(inputValue) { return inputValue.replace(new RegExp("(" + escapeRegex(getBufferTemplate().join('')) + ")*$"), ""); } function clearOptionalTail(input) { var buffer = getBuffer(), tmpBuffer = buffer.slice(), testPos, pos; for (var pos = tmpBuffer.length - 1; pos >= 0; pos--) { if (getTest(pos).optionality) { if (!isMask(pos) || !isValid(pos, buffer[pos], true)) tmpBuffer.pop(); else break; } else break; } writeBuffer(input, tmpBuffer); } function unmaskedvalue($input, skipDatepickerCheck) { if ($input.data('_inputmask') && (skipDatepickerCheck === true || !$input.hasClass('hasDatepicker'))) { var umValue = $.map(getBuffer(), function (element, index) { return isMask(index) && isValid(index, element, true) ? element : null; }); var unmaskedValue = (isRTL ? umValue.reverse() : umValue).join(''); return $.isFunction(opts.onUnMask) ? opts.onUnMask.call($input, getBuffer().join(''), unmaskedValue, opts) : unmaskedValue; } else { return $input[0]._valueGet(); } } function TranslatePosition(pos) { if (isRTL && typeof pos == 'number' && (!opts.greedy || opts.placeholder != "")) { var bffrLght = getBuffer().length; pos = bffrLght - pos; } return pos; } function caret(input, begin, end) { var npt = input.jquery && input.length > 0 ? input[0] : input, range; if (typeof begin == 'number') { begin = TranslatePosition(begin); end = TranslatePosition(end); if (!$(npt).is(':visible')) { return; } end = (typeof end == 'number') ? end : begin; npt.scrollLeft = npt.scrollWidth; if (opts.insertMode == false && begin == end) end++; //set visualization for insert/overwrite mode if (npt.setSelectionRange) { npt.selectionStart = begin; npt.selectionEnd = end; } else if (npt.createTextRange) { range = npt.createTextRange(); range.collapse(true); range.moveEnd('character', end); range.moveStart('character', begin); range.select(); } } else { if (!$(input).is(':visible')) { return { "begin": 0, "end": 0 }; } if (npt.setSelectionRange) { begin = npt.selectionStart; end = npt.selectionEnd; } else if (document.selection && document.selection.createRange) { range = document.selection.createRange(); begin = 0 - range.duplicate().moveStart('character', -100000); end = begin + range.text.length; } begin = TranslatePosition(begin); end = TranslatePosition(end); return { "begin": begin, "end": end }; } } function isComplete(buffer) { //return true / false / undefined (repeat *) if ($.isFunction(opts.isComplete)) return opts.isComplete.call($el, buffer, opts); if (opts.repeat == "*") return undefined; var complete = false, aml = seekPrevious(getMaskLength()); if (getLastValidPosition() == aml) { complete = true; for (var i = 0; i <= aml; i++) { var mask = isMask(i); if ((mask && (buffer[i] == undefined || buffer[i] == getPlaceholder(i))) || (!mask && buffer[i] != getPlaceholder(i))) { complete = false; break; } } } return complete; } function isSelection(begin, end) { return isRTL ? (begin - end) > 1 || ((begin - end) == 1 && opts.insertMode) : (end - begin) > 1 || ((end - begin) == 1 && opts.insertMode); } function installEventRuler(npt) { var events = $._data(npt).events; $.each(events, function (eventType, eventHandlers) { $.each(eventHandlers, function (ndx, eventHandler) { if (eventHandler.namespace == "inputmask") { if (eventHandler.type != "setvalue") { var handler = eventHandler.handler; eventHandler.handler = function (e) { if (this.readOnly || this.disabled) e.preventDefault; else return handler.apply(this, arguments); }; } } }); }); } function patchValueProperty(npt) { function PatchValhook(type) { if ($.valHooks[type] == undefined || $.valHooks[type].inputmaskpatch != true) { var valueGet = $.valHooks[type] && $.valHooks[type].get ? $.valHooks[type].get : function (elem) { return elem.value; }; var valueSet = $.valHooks[type] && $.valHooks[type].set ? $.valHooks[type].set : function (elem, value) { elem.value = value; return elem; }; $.valHooks[type] = { get: function (elem) { var $elem = $(elem); if ($elem.data('_inputmask')) { if ($elem.data('_inputmask')['opts'].autoUnmask) return $elem.inputmask('unmaskedvalue'); else { var result = valueGet(elem), inputData = $elem.data('_inputmask'), maskset = inputData['maskset'], bufferTemplate = maskset['_buffer']; bufferTemplate = bufferTemplate ? bufferTemplate.join('') : ''; return result != bufferTemplate ? result : ''; } } else return valueGet(elem); }, set: function (elem, value) { var $elem = $(elem); var result = valueSet(elem, value); if ($elem.data('_inputmask')) $elem.triggerHandler('setvalue.inputmask'); return result; }, inputmaskpatch: true }; } } var valueProperty; if (Object.getOwnPropertyDescriptor) valueProperty = Object.getOwnPropertyDescriptor(npt, "value"); if (valueProperty && valueProperty.get) { if (!npt._valueGet) { var valueGet = valueProperty.get; var valueSet = valueProperty.set; npt._valueGet = function () { return isRTL ? valueGet.call(this).split('').reverse().join('') : valueGet.call(this); }; npt._valueSet = function (value) { valueSet.call(this, isRTL ? value.split('').reverse().join('') : value); }; Object.defineProperty(npt, "value", { get: function () { var $self = $(this), inputData = $(this).data('_inputmask'), maskset = inputData['maskset']; return inputData && inputData['opts'].autoUnmask ? $self.inputmask('unmaskedvalue') : valueGet.call(this) != maskset['_buffer'].join('') ? valueGet.call(this) : ''; }, set: function (value) { valueSet.call(this, value); $(this).triggerHandler('setvalue.inputmask'); } }); } } else if (document.__lookupGetter__ && npt.__lookupGetter__("value")) { if (!npt._valueGet) { var valueGet = npt.__lookupGetter__("value"); var valueSet = npt.__lookupSetter__("value"); npt._valueGet = function () { return isRTL ? valueGet.call(this).split('').reverse().join('') : valueGet.call(this); }; npt._valueSet = function (value) { valueSet.call(this, isRTL ? value.split('').reverse().join('') : value); }; npt.__defineGetter__("value", function () { var $self = $(this), inputData = $(this).data('_inputmask'), maskset = inputData['maskset']; return inputData && inputData['opts'].autoUnmask ? $self.inputmask('unmaskedvalue') : valueGet.call(this) != maskset['_buffer'].join('') ? valueGet.call(this) : ''; }); npt.__defineSetter__("value", function (value) { valueSet.call(this, value); $(this).triggerHandler('setvalue.inputmask'); }); } } else { if (!npt._valueGet) { npt._valueGet = function () { return isRTL ? this.value.split('').reverse().join('') : this.value; }; npt._valueSet = function (value) { this.value = isRTL ? value.split('').reverse().join('') : value; }; } PatchValhook(npt.type); } } //shift chars to left from start to end and put c at end position if defined function shiftL(start, end, c, maskJumps) { var buffer = getBuffer(); if (maskJumps !== false) //jumping over nonmask position while (!isMask(start) && start - 1 >= 0) start--; for (var i = start; i < end && i < getMaskLength() ; i++) { if (isMask(i)) { setPlaceholder(i); var j = seekNext(i); var p = getBufferElement(buffer, j); if (p != getPlaceholder(j)) { if (j < getMaskLength() && isValid(i, p, true) !== false && getTest(i).def == getTest(j).def) { setBufferElement(buffer, i, p); if (j < end) { setPlaceholder(j); //cleanup next position } } else { if (isMask(i)) break; } } } else { setPlaceholder(i); } } if (c != undefined) setBufferElement(buffer, seekPrevious(end), c); if (opts.greedy == false) { var trbuffer = truncateInput(buffer.join('')).split(''); buffer.length = trbuffer.length; for (var i = 0, bl = buffer.length; i < bl; i++) { buffer[i] = trbuffer[i]; } if (buffer.length == 0) getMaskSet()["buffer"] = getBufferTemplate().slice(); } return start; //return the used start position } function shiftR(start, end, c) { var buffer = getBuffer(); if (getBufferElement(buffer, start) != getPlaceholder(start)) { for (var i = seekPrevious(end) ; i > start && i >= 0; i--) { if (isMask(i)) { var j = seekPrevious(i); var t = getBufferElement(buffer, j); if (t != getPlaceholder(j)) { if (isValid(i, t, true) !== false && getTest(i).def == getTest(j).def) { setBufferElement(buffer, i, t); setPlaceholder(j); } //else break; } } else setPlaceholder(i); } } if (c != undefined && getBufferElement(buffer, start) == getPlaceholder(start)) setBufferElement(buffer, start, c); var lengthBefore = buffer.length; if (opts.greedy == false) { var trbuffer = truncateInput(buffer.join('')).split(''); buffer.length = trbuffer.length; for (var i = 0, bl = buffer.length; i < bl; i++) { buffer[i] = trbuffer[i]; } if (buffer.length == 0) getMaskSet()["buffer"] = getBufferTemplate().slice(); } return end - (lengthBefore - buffer.length); //return new start position } function HandleRemove(input, k, pos) { if (opts.numericInput || isRTL) { switch (k) { case opts.keyCode.BACKSPACE: k = opts.keyCode.DELETE; break; case opts.keyCode.DELETE: k = opts.keyCode.BACKSPACE; break; } if (isRTL) { var pend = pos.end; pos.end = pos.begin; pos.begin = pend; } } var isSelection = true; if (pos.begin == pos.end) { var posBegin = k == opts.keyCode.BACKSPACE ? pos.begin - 1 : pos.begin; if (opts.isNumeric && opts.radixPoint != "" && getBuffer()[posBegin] == opts.radixPoint) { pos.begin = (getBuffer().length - 1 == posBegin) /* radixPoint is latest? delete it */ ? pos.begin : k == opts.keyCode.BACKSPACE ? posBegin : seekNext(posBegin); pos.end = pos.begin; } isSelection = false; if (k == opts.keyCode.BACKSPACE) pos.begin--; else if (k == opts.keyCode.DELETE) pos.end++; } else if (pos.end - pos.begin == 1 && !opts.insertMode) { isSelection = false; if (k == opts.keyCode.BACKSPACE) pos.begin--; } clearBuffer(getBuffer(), pos.begin, pos.end); var ml = getMaskLength(); if (opts.greedy == false && (isNaN(opts.repeat) || opts.repeat > 0)) { shiftL(pos.begin, ml, undefined, !isRTL && (k == opts.keyCode.BACKSPACE && !isSelection)); } else { var newpos = pos.begin; for (var i = pos.begin; i < pos.end; i++) { //seeknext to skip placeholders at start in selection if (isMask(i) || !isSelection) newpos = shiftL(pos.begin, ml, undefined, !isRTL && (k == opts.keyCode.BACKSPACE && !isSelection)); } if (!isSelection) pos.begin = newpos; } var firstMaskPos = seekNext(-1); clearBuffer(getBuffer(), pos.begin, pos.end, true); checkVal(input, false, false, getBuffer()); if (getLastValidPosition() < firstMaskPos) { getMaskSet()["p"] = firstMaskPos; } else { getMaskSet()["p"] = pos.begin; } } function keydownEvent(e) { //Safari 5.1.x - modal dialog fires keypress twice workaround skipKeyPressEvent = false; var input = this, $input = $(input), k = e.keyCode, pos = caret(input); //backspace, delete, and escape get special treatment if (k == opts.keyCode.BACKSPACE || k == opts.keyCode.DELETE || (iphone && k == 127) || e.ctrlKey && k == 88) { //backspace/delete e.preventDefault(); //stop default action but allow propagation if (k == 88) valueOnFocus = getBuffer().join(''); HandleRemove(input, k, pos); writeBuffer(input, getBuffer(), getMaskSet()["p"]); if (input._valueGet() == getBufferTemplate().join('')) $input.trigger('cleared'); if (opts.showTooltip) { //update tooltip $input.prop("title", getMaskSet()["mask"]); } } else if (k == opts.keyCode.END || k == opts.keyCode.PAGE_DOWN) { //when END or PAGE_DOWN pressed set position at lastmatch setTimeout(function () { var caretPos = seekNext(getLastValidPosition()); if (!opts.insertMode && caretPos == getMaskLength() && !e.shiftKey) caretPos--; caret(input, e.shiftKey ? pos.begin : caretPos, caretPos); }, 0); } else if ((k == opts.keyCode.HOME && !e.shiftKey) || k == opts.keyCode.PAGE_UP) { //Home or page_up caret(input, 0, e.shiftKey ? pos.begin : 0); } else if (k == opts.keyCode.ESCAPE || (k == 90 && e.ctrlKey)) { //escape && undo checkVal(input, true, false, valueOnFocus.split('')); $input.click(); } else if (k == opts.keyCode.INSERT && !(e.shiftKey || e.ctrlKey)) { //insert opts.insertMode = !opts.insertMode; caret(input, !opts.insertMode && pos.begin == getMaskLength() ? pos.begin - 1 : pos.begin); } else if (opts.insertMode == false && !e.shiftKey) { if (k == opts.keyCode.RIGHT) { setTimeout(function () { var caretPos = caret(input); caret(input, caretPos.begin); }, 0); } else if (k == opts.keyCode.LEFT) { setTimeout(function () { var caretPos = caret(input); caret(input, caretPos.begin - 1); }, 0); } } var currentCaretPos = caret(input); if (opts.onKeyDown.call(this, e, getBuffer(), opts) === true) //extra stuff to execute on keydown caret(input, currentCaretPos.begin, currentCaretPos.end); ignorable = $.inArray(k, opts.ignorables) != -1; } function keypressEvent(e, checkval, k, writeOut, strict, ndx) { //Safari 5.1.x - modal dialog fires keypress twice workaround if (k == undefined && skipKeyPressEvent) return false; skipKeyPressEvent = true; var input = this, $input = $(input); e = e || window.event; var k = checkval ? k : (e.which || e.charCode || e.keyCode); if (checkval !== true && (!(e.ctrlKey && e.altKey) && (e.ctrlKey || e.metaKey || ignorable))) { return true; } else { if (k) { //special treat the decimal separator if (checkval !== true && k == 46 && e.shiftKey == false && opts.radixPoint == ",") k = 44; var pos, forwardPosition, c = String.fromCharCode(k); if (checkval) { var pcaret = strict ? ndx : getLastValidPosition() + 1; pos = { begin: pcaret, end: pcaret }; } else { pos = caret(input); } //should we clear a possible selection?? var isSlctn = isSelection(pos.begin, pos.end); if (isSlctn) { getMaskSet()["undoBuffer"] = getBuffer().join(''); //init undobuffer for recovery when not valid HandleRemove(input, opts.keyCode.DELETE, pos); if (!opts.insertMode) { //preserve some space shiftR(pos.begin, getMaskLength()); } } var radixPosition = getBuffer().join('').indexOf(opts.radixPoint); if (opts.isNumeric && checkval !== true && radixPosition != -1) { if (opts.greedy && pos.begin <= radixPosition) { pos.begin = seekPrevious(pos.begin); pos.end = pos.begin; } else if (c == opts.radixPoint) { pos.begin = radixPosition; pos.end = pos.begin; } } getMaskSet()["writeOutBuffer"] = true; var p = pos.begin; var valResult = isValid(p, c, strict); if (valResult !== false) { var refresh = false, buffer = getBuffer(); if (valResult !== true) { refresh = valResult["refresh"]; //only rewrite buffer from isValid p = valResult.pos != undefined ? valResult.pos : p; //set new position from isValid c = valResult.c != undefined ? valResult.c : c; //set new char from isValid } if (refresh !== true) { if (opts.insertMode == true) { var freePos = p; if (getBufferElement(buffer, p) != getPlaceholder(p)) { while (getMaskSet()["validPositions"][freePos]) { freePos = seekNext(freePos); } } if (freePos < getMaskLength()) { shiftR(p, getMaskLength(), c); } else getMaskSet()["writeOutBuffer"] = false; } else setBufferElement(buffer, p, c); } forwardPosition = seekNext(p); getMaskSet()["p"] = forwardPosition; //needed for checkval } if (writeOut !== false) { var self = this; setTimeout(function () { opts.onKeyValidation.call(self, valResult, opts); }, 0); if (getMaskSet()["writeOutBuffer"] && valResult !== false) { var buffer = getBuffer(); var newCaretPosition; if (checkval) { newCaretPosition = undefined; } else if (opts.numericInput) { if (p > radixPosition) { newCaretPosition = seekPrevious(forwardPosition); } else if (c == opts.radixPoint) { newCaretPosition = forwardPosition - 1; } else newCaretPosition = seekPrevious(forwardPosition - 1); } else { newCaretPosition = forwardPosition; } writeBuffer(input, buffer, newCaretPosition); if (checkval !== true) { setTimeout(function () { //timeout needed for IE if (isComplete(buffer) === true) $input.trigger("complete"); skipInputEvent = true; $input.trigger("input"); }, 0); } } else if (isSlctn) { //TODO FIXME getMaskSet()["buffer"] = getMaskSet()["undoBuffer"].split(''); } } else if (isSlctn) { //TODO FIXME getMaskSet()["buffer"] = getMaskSet()["undoBuffer"].split(''); } if (opts.showTooltip) { //update tooltip $input.prop("title", getMaskSet()["mask"]); } //needed for IE8 and below if (e) e.preventDefault ? e.preventDefault() : e.returnValue = false; } } } function keyupEvent(e) { var $input = $(this), input = this, k = e.keyCode, buffer = getBuffer(); opts.onKeyUp.call(this, e, buffer, opts); //extra stuff to execute on keyup if (k == opts.keyCode.TAB && opts.showMaskOnFocus) { if ($input.hasClass('focus.inputmask') && input._valueGet().length == 0) { buffer = getBufferTemplate().slice(); writeBuffer(input, buffer); caret(input, 0); valueOnFocus = getBuffer().join(''); } else { writeBuffer(input, buffer); if (buffer.join('') == getBufferTemplate().join('') && $.inArray(opts.radixPoint, buffer) != -1) { caret(input, TranslatePosition(0)); $input.click(); } else caret(input, TranslatePosition(0), TranslatePosition(getMaskLength())); } } } function pasteEvent(e) { if (skipInputEvent === true && e.type == "input") { skipInputEvent = false; return true; } var input = this, $input = $(input); //paste event for IE8 and lower I guess ;-) if (e.type == "propertychange" && input._valueGet().length <= getMaskLength()) { return true; } setTimeout(function () { var pasteValue = $.isFunction(opts.onBeforePaste) ? opts.onBeforePaste.call(input, input._valueGet(), opts) : input._valueGet(); checkVal(input, false, false, pasteValue.split(''), true); writeBuffer(input, getBuffer()); if (isComplete(getBuffer()) === true) $input.trigger("complete"); $input.click(); }, 0); } //not used - attempt to support android function mobileInputEvent(e) { var input = this, $input = $(input); //backspace in chrome32 only fires input event - detect & treat var caretPos = caret(input), currentValue = input._valueGet(); currentValue = currentValue.replace(new RegExp("(" + escapeRegex(getBufferTemplate().join('')) + ")*"), ""); //correct caretposition for chrome if (caretPos.begin > currentValue.length) { caret(input, currentValue.length); caretPos = caret(input); } if ((getBuffer().length - currentValue.length) == 1 && currentValue.charAt(caretPos.begin) != getBuffer()[caretPos.begin] && currentValue.charAt(caretPos.begin + 1) != getBuffer()[caretPos.begin] && !isMask(caretPos.begin)) { e.keyCode = opts.keyCode.BACKSPACE; keydownEvent.call(input, e); } else { //nonnumerics don't fire keypress checkVal(input, false, false, currentValue.split('')); writeBuffer(input, getBuffer()); if (isComplete(getBuffer()) === true) $input.trigger("complete"); $input.click(); } e.preventDefault(); } function mask(el) { $el = $(el); if ($el.is(":input")) { //store tests & original buffer in the input element - used to get the unmasked value $el.data('_inputmask', { 'maskset': maskset, 'opts': opts, 'isRTL': false }); //show tooltip if (opts.showTooltip) { $el.prop("title", getMaskSet()["mask"]); } //correct greedy setting if needed opts.greedy = opts.greedy ? opts.greedy : opts.repeat == 0; patchValueProperty(el); if (opts.numericInput) opts.isNumeric = opts.numericInput; if (el.dir == "rtl" || (opts.numericInput && opts.rightAlignNumerics) || (opts.isNumeric && opts.rightAlignNumerics)) $el.css("text-align", "right"); if (el.dir == "rtl" || opts.numericInput) { el.dir = "ltr"; $el.removeAttr("dir"); var inputData = $el.data('_inputmask'); inputData['isRTL'] = true; $el.data('_inputmask', inputData); isRTL = true; } //unbind all events - to make sure that no other mask will interfere when re-masking $el.unbind(".inputmask"); $el.removeClass('focus.inputmask'); //bind events $el.closest('form').bind("submit", function () { //trigger change on submit if any if (valueOnFocus != getBuffer().join('')) { $el.change(); } }).bind('reset', function () { setTimeout(function () { $el.trigger("setvalue"); }, 0); }); $el.bind("mouseenter.inputmask", function () { var $input = $(this), input = this; if (!$input.hasClass('focus.inputmask') && opts.showMaskOnHover) { if (input._valueGet() != getBuffer().join('')) { writeBuffer(input, getBuffer()); } } }).bind("blur.inputmask", function () { var $input = $(this), input = this, nptValue = input._valueGet(), buffer = getBuffer(); $input.removeClass('focus.inputmask'); if (valueOnFocus != getBuffer().join('')) { $input.change(); } if (opts.clearMaskOnLostFocus && nptValue != '') { if (nptValue == getBufferTemplate().join('')) input._valueSet(''); else { //clearout optional tail of the mask clearOptionalTail(input); } } if (isComplete(buffer) === false) { $input.trigger("incomplete"); if (opts.clearIncomplete) { resetMaskSet(); if (opts.clearMaskOnLostFocus) input._valueSet(''); else { buffer = getBufferTemplate().slice(); writeBuffer(input, buffer); } } } }).bind("focus.inputmask", function () { var $input = $(this), input = this, nptValue = input._valueGet(); if (opts.showMaskOnFocus && !$input.hasClass('focus.inputmask') && (!opts.showMaskOnHover || (opts.showMaskOnHover && nptValue == ''))) { if (input._valueGet() != getBuffer().join('')) { writeBuffer(input, getBuffer(), seekNext(getLastValidPosition())); } } $input.addClass('focus.inputmask'); valueOnFocus = getBuffer().join(''); }).bind("mouseleave.inputmask", function () { var $input = $(this), input = this; if (opts.clearMaskOnLostFocus) { if (!$input.hasClass('focus.inputmask') && input._valueGet() != $input.attr("placeholder")) { if (input._valueGet() == getBufferTemplate().join('') || input._valueGet() == '') input._valueSet(''); else { //clearout optional tail of the mask clearOptionalTail(input); } } } }).bind("click.inputmask", function () { var input = this; setTimeout(function () { var selectedCaret = caret(input), buffer = getBuffer(); if (selectedCaret.begin == selectedCaret.end) { var clickPosition = isRTL ? TranslatePosition(selectedCaret.begin) : selectedCaret.begin, lvp = getLastValidPosition(undefined, clickPosition), lastPosition; if (opts.isNumeric) { lastPosition = opts.skipRadixDance === false && opts.radixPoint != "" && $.inArray(opts.radixPoint, buffer) != -1 ? (opts.numericInput ? seekNext($.inArray(opts.radixPoint, buffer)) : $.inArray(opts.radixPoint, buffer)) : seekNext(lvp); } else { lastPosition = seekNext(lvp); } if (clickPosition < lastPosition) { if (isMask(clickPosition)) caret(input, clickPosition); else caret(input, seekNext(clickPosition)); } else caret(input, lastPosition); } }, 0); }).bind('dblclick.inputmask', function () { var input = this; setTimeout(function () { caret(input, 0, seekNext(getLastValidPosition())); }, 0); }).bind(PasteEventType + ".inputmask dragdrop.inputmask drop.inputmask", pasteEvent ).bind('setvalue.inputmask', function () { var input = this; checkVal(input, true); valueOnFocus = getBuffer().join(''); if (input._valueGet() == getBufferTemplate().join('')) input._valueSet(''); }).bind('complete.inputmask', opts.oncomplete ).bind('incomplete.inputmask', opts.onincomplete ).bind('cleared.inputmask', opts.oncleared); $el.bind("keydown.inputmask", keydownEvent ).bind("keypress.inputmask", keypressEvent ).bind("keyup.inputmask", keyupEvent); // as the other inputevents aren't reliable for the moment we only base on the input event // needs follow-up if (android || androidfirefox || androidchrome || kindle) { $el.attr("autocomplete", "off") .attr("autocorrect", "off") .attr("autocapitalize", "off") .attr("spellcheck", false); if (androidfirefox || kindle) { $el.unbind("keydown.inputmask", keydownEvent ).unbind("keypress.inputmask", keypressEvent ).unbind("keyup.inputmask", keyupEvent); if (PasteEventType == "input") { $el.unbind(PasteEventType + ".inputmask"); } $el.bind("input.inputmask", mobileInputEvent); } } if (msie1x) $el.bind("input.inputmask", pasteEvent); //apply mask var initialValue = $.isFunction(opts.onBeforeMask) ? opts.onBeforeMask.call(el, el._valueGet(), opts) : el._valueGet(); checkVal(el, true, false, initialValue.split(''), true); valueOnFocus = getBuffer().join(''); // Wrap document.activeElement in a try/catch block since IE9 throw "Unspecified error" if document.activeElement is undefined when we are in an IFrame. var activeElement; try { activeElement = document.activeElement; } catch (e) { } if (activeElement === el) { //position the caret when in focus $el.addClass('focus.inputmask'); caret(el, seekNext(getLastValidPosition())); } else if (opts.clearMaskOnLostFocus) { if (getBuffer().join('') == getBufferTemplate().join('')) { el._valueSet(''); } else { clearOptionalTail(el); } } else { writeBuffer(el, getBuffer()); } installEventRuler(el); } } //action object if (actionObj != undefined) { switch (actionObj["action"]) { case "isComplete": return isComplete(actionObj["buffer"]); case "unmaskedvalue": isRTL = actionObj["$input"].data('_inputmask')['isRTL']; return unmaskedvalue(actionObj["$input"], actionObj["skipDatepickerCheck"]); case "mask": mask(actionObj["el"]); break; case "format": $el = $({}); $el.data('_inputmask', { 'maskset': maskset, 'opts': opts, 'isRTL': opts.numericInput }); if (opts.numericInput) { opts.isNumeric = opts.numericInput; isRTL = true; } checkVal($el, false, false, actionObj["value"].split(''), true); return getBuffer().join(''); case "isValid": $el = $({}); $el.data('_inputmask', { 'maskset': maskset, 'opts': opts, 'isRTL': opts.numericInput }); if (opts.numericInput) { opts.isNumeric = opts.numericInput; isRTL = true; } checkVal($el, false, true, actionObj["value"].split('')); return isComplete(getBuffer()); } } }; $.inputmask = { //options default defaults: { placeholder: "_", optionalmarker: { start: "[", end: "]" }, quantifiermarker: { start: "{", end: "}" }, groupmarker: { start: "(", end: ")" }, escapeChar: "\\", mask: null, oncomplete: $.noop, //executes when the mask is complete onincomplete: $.noop, //executes when the mask is incomplete and focus is lost oncleared: $.noop, //executes when the mask is cleared repeat: 0, //repetitions of the mask: * ~ forever, otherwise specify an integer greedy: true, //true: allocated buffer for the mask and repetitions - false: allocate only if needed autoUnmask: false, //automatically unmask when retrieving the value with $.fn.val or value if the browser supports __lookupGetter__ or getOwnPropertyDescriptor clearMaskOnLostFocus: true, insertMode: true, //insert the input or overwrite the input clearIncomplete: false, //clear the incomplete input on blur aliases: {}, //aliases definitions => see jquery.inputmask.extensions.js onKeyUp: $.noop, //override to implement autocomplete on certain keys for example onKeyDown: $.noop, //override to implement autocomplete on certain keys for example onBeforeMask: undefined, //executes before masking the initial value to allow preprocessing of the initial value. args => initialValue, opts => return processedValue onBeforePaste: undefined, //executes before masking the pasted value to allow preprocessing of the pasted value. args => pastedValue, opts => return processedValue onUnMask: undefined, //executes after unmasking to allow postprocessing of the unmaskedvalue. args => maskedValue, unmaskedValue, opts showMaskOnFocus: true, //show the mask-placeholder when the input has focus showMaskOnHover: true, //show the mask-placeholder when hovering the empty input onKeyValidation: $.noop, //executes on every key-press with the result of isValid. Params: result, opts skipOptionalPartCharacter: " ", //a character which can be used to skip an optional part of a mask showTooltip: false, //show the activemask as tooltip numericInput: false, //numericInput input direction style (input shifts to the left while holding the caret position) //numeric basic properties isNumeric: false, //enable numeric features radixPoint: "", //".", // | "," skipRadixDance: false, //disable radixpoint caret positioning rightAlignNumerics: true, //align numerics to the right //numeric basic properties definitions: { '9': { validator: "[0-9]", cardinality: 1, definitionSymbol: "*" }, 'a': { validator: "[A-Za-z\u0410-\u044F\u0401\u0451]", cardinality: 1, definitionSymbol: "*" }, '*': { validator: "[A-Za-z\u0410-\u044F\u0401\u04510-9]", cardinality: 1 } }, keyCode: { ALT: 18, BACKSPACE: 8, CAPS_LOCK: 20, COMMA: 188, COMMAND: 91, COMMAND_LEFT: 91, COMMAND_RIGHT: 93, CONTROL: 17, DELETE: 46, DOWN: 40, END: 35, ENTER: 13, ESCAPE: 27, HOME: 36, INSERT: 45, LEFT: 37, MENU: 93, NUMPAD_ADD: 107, NUMPAD_DECIMAL: 110, NUMPAD_DIVIDE: 111, NUMPAD_ENTER: 108, NUMPAD_MULTIPLY: 106, NUMPAD_SUBTRACT: 109, PAGE_DOWN: 34, PAGE_UP: 33, PERIOD: 190, RIGHT: 39, SHIFT: 16, SPACE: 32, TAB: 9, UP: 38, WINDOWS: 91 }, //specify keycodes which should not be considered in the keypress event, otherwise the preventDefault will stop their default behavior especially in FF ignorables: [8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123], isComplete: undefined //override for isComplete - args => buffer, opts - return true || false }, escapeRegex: function (str) { var specials = ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\']; return str.replace(new RegExp('(\\' + specials.join('|\\') + ')', 'gim'), '\\$1'); }, format: function (value, options) { var opts = $.extend(true, {}, $.inputmask.defaults, options); resolveAlias(opts.alias, options, opts); return maskScope(generateMaskSet(opts), opts, { "action": "format", "value": value }); }, isValid: function (value, options) { var opts = $.extend(true, {}, $.inputmask.defaults, options); resolveAlias(opts.alias, options, opts); return maskScope(generateMaskSet(opts), opts, { "action": "isValid", "value": value }); } }; $.fn.inputmask = function (fn, options) { var opts = $.extend(true, {}, $.inputmask.defaults, options), maskset; if (typeof fn === "string") { switch (fn) { case "mask": //resolve possible aliases given by options resolveAlias(opts.alias, options, opts); maskset = generateMaskSet(opts); if (maskset.length == 0) { return this; } return this.each(function () { maskScope($.extend(true, {}, maskset), 0, opts, { "action": "mask", "el": this }); }); case "unmaskedvalue": var $input = $(this), input = this; if ($input.data('_inputmask')) { maskset = $input.data('_inputmask')['maskset']; opts = $input.data('_inputmask')['opts']; return maskScope(maskset, opts, { "action": "unmaskedvalue", "$input": $input }); } else return $input.val(); case "remove": return this.each(function () { var $input = $(this), input = this; if ($input.data('_inputmask')) { maskset = $input.data('_inputmask')['maskset']; opts = $input.data('_inputmask')['opts']; //writeout the unmaskedvalue input._valueSet(maskScope(maskset, opts, { "action": "unmaskedvalue", "$input": $input, "skipDatepickerCheck": true })); //clear data $input.removeData('_inputmask'); //unbind all events $input.unbind(".inputmask"); $input.removeClass('focus.inputmask'); //restore the value property var valueProperty; if (Object.getOwnPropertyDescriptor) valueProperty = Object.getOwnPropertyDescriptor(input, "value"); if (valueProperty && valueProperty.get) { if (input._valueGet) { Object.defineProperty(input, "value", { get: input._valueGet, set: input._valueSet }); } } else if (document.__lookupGetter__ && input.__lookupGetter__("value")) { if (input._valueGet) { input.__defineGetter__("value", input._valueGet); input.__defineSetter__("value", input._valueSet); } } try { //try catch needed for IE7 as it does not supports deleting fns delete input._valueGet; delete input._valueSet; } catch (e) { input._valueGet = undefined; input._valueSet = undefined; } } }); break; case "getemptymask": //return the default (empty) mask value, usefull for setting the default value in validation if (this.data('_inputmask')) { maskset = this.data('_inputmask')['maskset']; return maskset['_buffer'].join(''); } else return ""; case "hasMaskedValue": //check wheter the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value return this.data('_inputmask') ? !this.data('_inputmask')['opts'].autoUnmask : false; case "isComplete": maskset = this.data('_inputmask')['maskset']; opts = this.data('_inputmask')['opts']; return maskScope(maskset, opts, { "action": "isComplete", "buffer": this[0]._valueGet().split('') }); case "getmetadata": //return mask metadata if exists if (this.data('_inputmask')) { maskset = this.data('_inputmask')['maskset']; return maskset['metadata']; } else return undefined; default: //check if the fn is an alias if (!resolveAlias(fn, options, opts)) { //maybe fn is a mask so we try //set mask opts.mask = fn; } maskset = generateMaskSet(opts); if (maskset == undefined) { return this; } return this.each(function () { maskScope($.extend(true, {}, maskset), opts, { "action": "mask", "el": this }); }); break; } } else if (typeof fn == "object") { opts = $.extend(true, {}, $.inputmask.defaults, fn); resolveAlias(opts.alias, fn, opts); //resolve aliases maskset = generateMaskSet(opts); if (maskset == undefined) { return this; } return this.each(function () { maskScope($.extend(true, {}, maskset), opts, { "action": "mask", "el": this }); }); } else if (fn == undefined) { //look for data-inputmask atribute - the attribute should only contain optipns return this.each(function () { var attrOptions = $(this).attr("data-inputmask"); if (attrOptions && attrOptions != "") { try { attrOptions = attrOptions.replace(new RegExp("'", "g"), '"'); var dataoptions = $.parseJSON("{" + attrOptions + "}"); $.extend(true, dataoptions, options); opts = $.extend(true, {}, $.inputmask.defaults, dataoptions); resolveAlias(opts.alias, dataoptions, opts); opts.alias = undefined; $(this).inputmask(opts); } catch (ex) { } //need a more relax parseJSON } }); } }; } })(jQuery);