|
|
@@ -3,7 +3,7 @@ Input Mask plugin for jquery
|
|
|
http://github.com/RobinHerbots/jquery.inputmask
|
|
|
Copyright (c) 2010 Robin Herbots
|
|
|
Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
|
|
|
-Version: 0.4.8 - dev
|
|
|
+Version: 0.4.9 - dev
|
|
|
|
|
|
This plugin is based on the masked input plugin written by Josh Bush (digitalbush.com)
|
|
|
*/
|
|
|
@@ -48,7 +48,8 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
},
|
|
|
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
|
|
|
- }
|
|
|
+ },
|
|
|
+ ignorables: [8, 9, 13, 16, 17, 18, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 91, 93, 106, 107, 108, 109, 110, 111]
|
|
|
},
|
|
|
val: $.fn.val //store the original jquery val function
|
|
|
};
|
|
|
@@ -59,12 +60,12 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
|
|
|
var iphone = navigator.userAgent.match(/iphone/i) != null;
|
|
|
var android = navigator.userAgent.match(/android.*mobile safari.*/i) != null;
|
|
|
- if (android){
|
|
|
- var browser = navigator.userAgent.match(/mobile safari.*/i);
|
|
|
- var version = parseInt(new RegExp(/[0-9]+/).exec(browser));
|
|
|
- android = version <= 533;
|
|
|
- }
|
|
|
- var caretposCorrection = null;
|
|
|
+ if (android) {
|
|
|
+ var browser = navigator.userAgent.match(/mobile safari.*/i);
|
|
|
+ var version = parseInt(new RegExp(/[0-9]+/).exec(browser));
|
|
|
+ android = version <= 533;
|
|
|
+ }
|
|
|
+ var caretposCorrection = null;
|
|
|
|
|
|
var _val = $.inputmask.val;
|
|
|
if (opts.patch_val && $.fn.val.inputmaskpatch != true) {
|
|
|
@@ -321,7 +322,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
function prepareBuffer(buffer, position) {
|
|
|
while ((buffer.length <= position || position < 0) && buffer.length < getMaskLength()) {
|
|
|
var j = 0;
|
|
|
- if (opts.numericInput) {
|
|
|
+ if (isRTL) {
|
|
|
j = determineTestPosition(position);
|
|
|
buffer.unshift(_buffer[j]);
|
|
|
} else while (_buffer[j] !== undefined) {
|
|
|
@@ -356,62 +357,36 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
var inputValue = TruncateInput(_val.call(input));
|
|
|
clearBuffer(buffer, 0, buffer.length);
|
|
|
buffer.length = _buffer.length;
|
|
|
- var lastMatch = -1, checkPosition = -1, maskL = getMaskLength(), ivl = inputValue.length;
|
|
|
- if (opts.numericInput) {
|
|
|
- lastMatch += maskL;
|
|
|
- var p = seekPrevious(buffer, ivl);
|
|
|
- for (var ivp = 0; ivp < ivl; ivp++) {
|
|
|
- var c = inputValue.charAt(ivp);
|
|
|
- if (isValid(p, c, buffer)) {
|
|
|
- for (var i = 0; i < maskL; i++) {
|
|
|
- if (isMask(i)) {
|
|
|
- SetReTargetPlaceHolder(buffer, i);
|
|
|
-
|
|
|
- var j = seekNext(buffer, i);
|
|
|
- var el = getBufferElement(buffer, j);
|
|
|
- if (el != getPlaceHolder(j)) {
|
|
|
- if (j < getMaskLength() && isValid(i, el, buffer) !== false) {
|
|
|
- setBufferElement(buffer, i, getBufferElement(buffer, j));
|
|
|
- } else {
|
|
|
- if (isMask(i))
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- } else
|
|
|
- SetReTargetPlaceHolder(buffer, i);
|
|
|
- }
|
|
|
- lastMatch = seekPrevious(buffer, maskL);
|
|
|
- setBufferElement(buffer, lastMatch, c);
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- for (var i = 0; i < ivl; i++) {
|
|
|
- for (var pos = checkPosition + 1; pos < maskL; pos++) {
|
|
|
- if (isMask(pos)) {
|
|
|
- if (isValid(pos, inputValue.charAt(i), buffer) !== false) {
|
|
|
- setBufferElement(buffer, pos, inputValue.charAt(i));
|
|
|
- lastMatch = checkPosition = pos;
|
|
|
- } else {
|
|
|
- SetReTargetPlaceHolder(buffer, pos);
|
|
|
- if (isMask(i) && inputValue.charAt(i) == getPlaceHolder(i))
|
|
|
- checkPosition = pos;
|
|
|
- }
|
|
|
- break;
|
|
|
- } else { //nonmask
|
|
|
+ var lastMatch = -1, checkPosition = -1, maskL = getMaskLength(), ivl = inputValue.length, rtlMatch = ivl == 0 ? maskL : -1;
|
|
|
+ for (var i = 0; i < ivl; i++) {
|
|
|
+ for (var pos = checkPosition + 1; pos < maskL; pos++) {
|
|
|
+ if (isMask(pos)) {
|
|
|
+ var c = inputValue.charAt(i);
|
|
|
+ if (isValid(pos, c, buffer) !== false) {
|
|
|
+ setBufferElement(buffer, pos, c);
|
|
|
+ lastMatch = checkPosition = pos;
|
|
|
+ } else {
|
|
|
SetReTargetPlaceHolder(buffer, pos);
|
|
|
- if (lastMatch == checkPosition) //once outsync the nonmask cannot be the lastmatch
|
|
|
- lastMatch = pos;
|
|
|
- checkPosition = pos;
|
|
|
- if (inputValue.charAt(i) == getBufferElement(buffer, pos))
|
|
|
- break;
|
|
|
+ if (isMask(i) && c == getPlaceHolder(i)) {
|
|
|
+ checkPosition = pos;
|
|
|
+ rtlMatch = pos;
|
|
|
+ }
|
|
|
}
|
|
|
+ break;
|
|
|
+ } else { //nonmask
|
|
|
+ SetReTargetPlaceHolder(buffer, pos);
|
|
|
+ if (lastMatch == checkPosition) //once outsync the nonmask cannot be the lastmatch
|
|
|
+ lastMatch = pos;
|
|
|
+ checkPosition = pos;
|
|
|
+ if (inputValue.charAt(i) == getBufferElement(buffer, pos))
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (clearInvalid) {
|
|
|
writeBuffer(input, buffer);
|
|
|
}
|
|
|
- return seekNext(buffer, lastMatch);
|
|
|
+ return seekNext(buffer, isRTL ? rtlMatch : lastMatch);
|
|
|
}
|
|
|
|
|
|
function EscapeRegex(str) {
|
|
|
@@ -471,13 +446,14 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
end = begin + range.text.length;
|
|
|
}
|
|
|
caretpos = { begin: begin, end: end };
|
|
|
- }
|
|
|
+ }
|
|
|
return caretpos;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
function mask(el) {
|
|
|
var input = $(el);
|
|
|
+
|
|
|
//store tests & original buffer in the input element - used to get the unmasked value
|
|
|
input.data('inputmask', {
|
|
|
'tests': tests,
|
|
|
@@ -493,7 +469,14 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
undoBuffer = _val.call(input),
|
|
|
skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
|
|
|
lastPosition = -1,
|
|
|
- firstMaskPos = seekNext(buffer, -1);
|
|
|
+ firstMaskPos = seekNext(buffer, -1)
|
|
|
+ isRTL = false;
|
|
|
+ if (el.dir == "rtl" || opts.numericInput) {
|
|
|
+ el.dir = "ltr"
|
|
|
+ input.css("text-align", "right");
|
|
|
+ input.removeAttr("dir");
|
|
|
+ isRTL = true;
|
|
|
+ }
|
|
|
|
|
|
//unbind all events - to make sure that no other mask will interfere when re-masking
|
|
|
input.unbind(".inputmask");
|
|
|
@@ -542,7 +525,10 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
if (selectedCaret.begin == selectedCaret.end) {
|
|
|
var clickPosition = selectedCaret.begin;
|
|
|
lastPosition = checkVal(input, buffer, false);
|
|
|
- caret(input, clickPosition < lastPosition && (isValid(clickPosition, buffer[clickPosition], buffer) || !isMask(clickPosition)) ? clickPosition : lastPosition);
|
|
|
+ if (isRTL)
|
|
|
+ caret(input, clickPosition > lastPosition && (isValid(clickPosition, buffer[clickPosition], buffer) || !isMask(clickPosition)) ? clickPosition : lastPosition);
|
|
|
+ else
|
|
|
+ caret(input, clickPosition < lastPosition && (isValid(clickPosition, buffer[clickPosition], buffer) || !isMask(clickPosition)) ? clickPosition : lastPosition);
|
|
|
}
|
|
|
}, 0);
|
|
|
}).bind('dblclick.inputmask', function() {
|
|
|
@@ -558,7 +544,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
if (k == opts.keyCode.TAB && input.hasClass('focus.inputmask') && _val.call(input).length == 0) {
|
|
|
buffer = _buffer.slice();
|
|
|
writeBuffer(input, buffer);
|
|
|
- if (!opts.numericInput) caret(input, 0);
|
|
|
+ if (!isRTL) caret(input, 0);
|
|
|
}
|
|
|
}).bind(pasteEventName, function() {
|
|
|
var input = $(this);
|
|
|
@@ -607,15 +593,15 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
}
|
|
|
}
|
|
|
if (c != undefined)
|
|
|
- setBufferElement(buffer, seekPrevious(buffer, end), c);
|
|
|
+ setBufferElement(buffer, isRTL ? end : seekPrevious(buffer, end), c);
|
|
|
|
|
|
buffer = TruncateInput(buffer.join('')).split('');
|
|
|
if (buffer.length == 0) buffer = _buffer.slice();
|
|
|
|
|
|
return start; //return the used start position
|
|
|
}
|
|
|
- function shiftR(pos, c, full) { //full => behave like a push right ~ do not stop on placeholders
|
|
|
- for (var i = pos; i < getMaskLength(); i++) {
|
|
|
+ function shiftR(start, end, c, full) { //full => behave like a push right ~ do not stop on placeholders
|
|
|
+ for (var i = start; i <= end && i < getMaskLength(); i++) {
|
|
|
if (isMask(i)) {
|
|
|
var t = getBufferElement(buffer, i);
|
|
|
setBufferElement(buffer, i, c);
|
|
|
@@ -637,12 +623,10 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
};
|
|
|
|
|
|
function keydownEvent(e) {
|
|
|
- var input = $(this);
|
|
|
-
|
|
|
//Safari 5.1.x - modal dialog fires keypress twice workaround
|
|
|
skipKeyPressEvent = false;
|
|
|
|
|
|
- var k = e.keyCode, pos = caret(input);
|
|
|
+ var input = $(this), k = e.keyCode, pos = caret(input);
|
|
|
|
|
|
//delete selection before proceeding
|
|
|
if (((pos.end - pos.begin) > 1 || ((pos.end - pos.begin) == 1 && opts.insertMode)) && (k == opts.keyCode.BACKSPACE || k == opts.keyCode.DELETE))
|
|
|
@@ -654,18 +638,16 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
if (pos.begin == 0 && pos.end == maskL) {
|
|
|
buffer = _buffer.slice();
|
|
|
writeBuffer(input, buffer);
|
|
|
- if (!opts.numericInput) caret(input, 0);
|
|
|
+ if (!isRTL) caret(input, 0);
|
|
|
} else {
|
|
|
var beginPos = pos.begin - (k == opts.keyCode.DELETE ? 0 : 1);
|
|
|
- beginPos = shiftL(beginPos < 0 ? 0 : beginPos, maskL);
|
|
|
- if (opts.numericInput) {
|
|
|
- shiftR(0, getPlaceHolder(0), true);
|
|
|
- beginPos = seekNext(buffer, beginPos);
|
|
|
- }
|
|
|
+ if (isRTL) {
|
|
|
+ shiftR(0, beginPos, getPlaceHolder(0), true);
|
|
|
+ if (k == opts.keyCode.BACKSPACE) {
|
|
|
+ beginPos++;
|
|
|
+ }
|
|
|
+ } else beginPos = shiftL(beginPos < 0 ? 0 : beginPos, maskL);
|
|
|
writeBuffer(input, buffer, beginPos);
|
|
|
- // if (!opts.insertMode && k == opts.keyCode.BACKSPACE) {
|
|
|
- // caret(input, beginPos);
|
|
|
- // }
|
|
|
}
|
|
|
if (opts.oncleared && _val.call(input) == _buffer.join(''))
|
|
|
opts.oncleared.call(input);
|
|
|
@@ -707,26 +689,25 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
}
|
|
|
|
|
|
function keypressEvent(e) {
|
|
|
- var input = $(this);
|
|
|
-
|
|
|
//Safari 5.1.x - modal dialog fires keypress twice workaround
|
|
|
if (skipKeyPressEvent) return false;
|
|
|
skipKeyPressEvent = true;
|
|
|
|
|
|
+ var input = $(this);
|
|
|
+
|
|
|
e = e || window.event;
|
|
|
- if (e.ctrlKey || e.altKey || e.metaKey) {//Ignore
|
|
|
+ var k = e.which || e.charCode || e.keyCode;
|
|
|
+ if (e.ctrlKey || e.altKey || e.metaKey || $.inArray(k, opts.ignorables) != -1) {//Ignore
|
|
|
return true;
|
|
|
} else {
|
|
|
- var k = e.which || e.charCode || e.keyCode;
|
|
|
if (k) {
|
|
|
- var pos = caret(input), c = String.fromCharCode(k);
|
|
|
- if (opts.numericInput) {
|
|
|
- var posEnd = opts.greedy ? pos.end : (pos.end + 1);
|
|
|
- var p = seekPrevious(buffer, posEnd);
|
|
|
+ var pos = caret(input), c = String.fromCharCode(k), maskL = getMaskLength();
|
|
|
+ if (isRTL) {
|
|
|
+ var p = seekPrevious(buffer, pos.end);
|
|
|
if (isValid(p, c, buffer)) {
|
|
|
- if (isValid(firstMaskPos, buffer[firstMaskPos], buffer) == false || (opts.greedy === false && buffer.length < getMaskLength())) {
|
|
|
- shiftL(firstMaskPos, posEnd, c);
|
|
|
- writeBuffer(input, buffer, posEnd);
|
|
|
+ if (isValid(firstMaskPos, buffer[firstMaskPos], buffer) == false || (opts.greedy === false && buffer.length < maskL)) {
|
|
|
+ shiftL(firstMaskPos, p, c);
|
|
|
+ writeBuffer(input, buffer, opts.numericInput ? pos.end : p);
|
|
|
} else if (opts.oncomplete)
|
|
|
opts.oncomplete.call(input);
|
|
|
} else if (android) writeBuffer(input, buffer, pos.begin);
|
|
|
@@ -734,18 +715,17 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
else {
|
|
|
var p = seekNext(buffer, pos.begin - 1);
|
|
|
if (isValid(p, c, buffer)) {
|
|
|
- if (opts.insertMode == true) shiftR(p, c); else setBufferElement(buffer, p, c);
|
|
|
+ if (opts.insertMode == true) shiftR(p, maskL, c); else setBufferElement(buffer, p, c);
|
|
|
var next = seekNext(buffer, p);
|
|
|
writeBuffer(input, buffer, next);
|
|
|
|
|
|
- if (opts.oncomplete && next == getMaskLength())
|
|
|
+ if (opts.oncomplete && next == maskL)
|
|
|
opts.oncomplete.call(input);
|
|
|
} else if (android) writeBuffer(input, buffer, pos.begin);
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- return false;
|
|
|
}
|
|
|
}
|
|
|
};
|