|
|
@@ -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.5.3
|
|
|
+Version: 0.5.3a
|
|
|
|
|
|
This plugin is based on the masked input plugin written by Josh Bush (digitalbush.com)
|
|
|
*/
|
|
|
@@ -88,28 +88,28 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
case "remove":
|
|
|
var tests, _buffer;
|
|
|
return this.each(function() {
|
|
|
- var input = $(this);
|
|
|
- if (input.data('inputmask')) {
|
|
|
- tests = input.data('inputmask')['tests'];
|
|
|
- _buffer = input.data('inputmask')['_buffer'];
|
|
|
- opts.greedy = input.data('inputmask')['greedy'];
|
|
|
- opts.repeat = input.data('inputmask')['repeat'];
|
|
|
- opts.definitions = input.data('inputmask')['definitions'];
|
|
|
+ var $input = $(this), input = this;
|
|
|
+ if ($input.data('inputmask')) {
|
|
|
+ tests = $input.data('inputmask')['tests'];
|
|
|
+ _buffer = $input.data('inputmask')['_buffer'];
|
|
|
+ opts.greedy = $input.data('inputmask')['greedy'];
|
|
|
+ opts.repeat = $input.data('inputmask')['repeat'];
|
|
|
+ opts.definitions = $input.data('inputmask')['definitions'];
|
|
|
//writeout the unmaskedvalue
|
|
|
- _val.call(input, unmaskedvalue(input, true));
|
|
|
+ input._valueSet(unmaskedvalue($input, true));
|
|
|
//clear data
|
|
|
- input.removeData('inputmask');
|
|
|
+ $input.removeData('inputmask');
|
|
|
//unbind all events
|
|
|
- input.unbind(".inputmask");
|
|
|
- input.removeClass('focus.inputmask');
|
|
|
+ $input.unbind(".inputmask");
|
|
|
+ $input.removeClass('focus.inputmask');
|
|
|
//restore the value property
|
|
|
if (document.__lookupGetter__) {
|
|
|
- if (this._valueGet) {
|
|
|
- this.__defineGetter__("value", this._valueGet);
|
|
|
- this.__defineSetter__("value", this._valueSet);
|
|
|
+ if (input._valueGet) {
|
|
|
+ input.__defineGetter__("value", input._valueGet);
|
|
|
+ input.__defineSetter__("value", input._valueSet);
|
|
|
|
|
|
- delete this._valueGet;
|
|
|
- delete this._valueSet;
|
|
|
+ delete input._valueGet;
|
|
|
+ delete input._valueSet;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -306,7 +306,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 (input.data('inputmask')['isRTL']) {
|
|
|
+ if ($input.data('inputmask')['isRTL']) {
|
|
|
j = _buffer.length - 1;
|
|
|
position = position + _buffer.length;
|
|
|
while (_buffer[j] !== undefined)
|
|
|
@@ -321,7 +321,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
}
|
|
|
|
|
|
function writeBuffer(input, buffer, caretPos) {
|
|
|
- _val.call(input, buffer.join(''));
|
|
|
+ input._valueSet(buffer.join(''));
|
|
|
if (caretPos != undefined) {
|
|
|
if (android) {
|
|
|
setTimeout(function() {
|
|
|
@@ -343,8 +343,8 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
}
|
|
|
|
|
|
function checkVal(input, buffer, clearInvalid) {
|
|
|
- var isRTL = input.data('inputmask')['isRTL'],
|
|
|
- inputValue = TruncateInput(_val.call(input), isRTL);
|
|
|
+ var isRTL = $(input).data('inputmask')['isRTL'],
|
|
|
+ inputValue = TruncateInput(input._valueGet(), isRTL);
|
|
|
clearBuffer(buffer, 0, buffer.length);
|
|
|
buffer.length = _buffer.length;
|
|
|
var lastMatch = -1, checkPosition = -1, maskL = getMaskLength(), ivl = inputValue.length, rtlMatch = ivl == 0 ? maskL : -1;
|
|
|
@@ -389,7 +389,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
|
|
|
function clearOptionalTail(input, buffer) {
|
|
|
var tmpBuffer = buffer.slice();
|
|
|
- if (input.data('inputmask')['isRTL']) {
|
|
|
+ if ($(input).data('inputmask')['isRTL']) {
|
|
|
for (var pos = 0; pos <= tmpBuffer.length - 1; pos++) {
|
|
|
var testPos = determineTestPosition(pos);
|
|
|
if (tests[testPos].optionality) {
|
|
|
@@ -419,17 +419,17 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
}
|
|
|
|
|
|
//functionality fn
|
|
|
- function unmaskedvalue(el, skipDatepickerCheck) {
|
|
|
-
|
|
|
- if (tests && (skipDatepickerCheck === true || !el.hasClass('hasDatepicker'))) {
|
|
|
+ function unmaskedvalue($input, skipDatepickerCheck) {
|
|
|
+ var input = $input[0];
|
|
|
+ if (tests && (skipDatepickerCheck === true || !$input.hasClass('hasDatepicker'))) {
|
|
|
var buffer = _buffer.slice();
|
|
|
- checkVal(el, buffer);
|
|
|
+ checkVal(input, buffer);
|
|
|
return $.map(buffer, function(element, index) {
|
|
|
return isMask(index) && element != getBufferElement(_buffer.slice(), index) ? element : null;
|
|
|
}).join('');
|
|
|
}
|
|
|
else {
|
|
|
- return _val.call(el);
|
|
|
+ return input._valueGet();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -467,10 +467,10 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
};
|
|
|
|
|
|
function mask(el) {
|
|
|
- var input = $(el);
|
|
|
+ var $input = $(el);
|
|
|
|
|
|
//store tests & original buffer in the input element - used to get the unmasked value
|
|
|
- input.data('inputmask', {
|
|
|
+ $input.data('inputmask', {
|
|
|
'tests': tests,
|
|
|
'_buffer': _buffer,
|
|
|
'greedy': opts.greedy,
|
|
|
@@ -484,7 +484,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
|
|
|
//init vars
|
|
|
var buffer = _buffer.slice(),
|
|
|
- undoBuffer = _val.call(input),
|
|
|
+ undoBuffer = el._valueGet(),
|
|
|
skipKeyPressEvent = false, //Safari 5.1.x - modal dialog fires keypress twice workaround
|
|
|
ignorable = false,
|
|
|
lastPosition = -1,
|
|
|
@@ -492,37 +492,38 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
isRTL = false;
|
|
|
if (el.dir == "rtl" || opts.numericInput) {
|
|
|
el.dir = "ltr"
|
|
|
- input.css("text-align", "right");
|
|
|
- input.removeAttr("dir");
|
|
|
- inputData = input.data('inputmask');
|
|
|
+ $input.css("text-align", "right");
|
|
|
+ $input.removeAttr("dir");
|
|
|
+ inputData = $input.data('inputmask');
|
|
|
inputData['isRTL'] = true;
|
|
|
- input.data('inputmask', inputData);
|
|
|
+ $input.data('inputmask', inputData);
|
|
|
isRTL = true;
|
|
|
}
|
|
|
|
|
|
//unbind all events - to make sure that no other mask will interfere when re-masking
|
|
|
- input.unbind(".inputmask");
|
|
|
- input.removeClass('focus.inputmask');
|
|
|
+ $input.unbind(".inputmask");
|
|
|
+ $input.removeClass('focus.inputmask');
|
|
|
//bind events
|
|
|
- if (!input.attr("readonly")) {
|
|
|
- input.bind("mouseenter.inputmask", function() {
|
|
|
- var input = $(this);
|
|
|
- if (!input.hasClass('focus.inputmask')) {
|
|
|
- if (_val.call(input).length == 0) {
|
|
|
+ if (!$input.attr("readonly")) {
|
|
|
+ $input.bind("mouseenter.inputmask", function() {
|
|
|
+ var $input = $(this), input = this;
|
|
|
+ if (!$input.hasClass('focus.inputmask')) {
|
|
|
+ var nptL = input._valueGet().length;
|
|
|
+ if (nptL == 0) {
|
|
|
buffer = _buffer.slice();
|
|
|
writeBuffer(input, buffer);
|
|
|
- } else if (_val.call(input).length < buffer.length)
|
|
|
+ } else if (nptL < buffer.length)
|
|
|
writeBuffer(input, buffer);
|
|
|
}
|
|
|
}).bind("blur.inputmask", function() {
|
|
|
- var input = $(this);
|
|
|
- input.removeClass('focus.inputmask');
|
|
|
- if (_val.call(input) != undoBuffer) {
|
|
|
- input.change();
|
|
|
+ var $input = $(this), input = this, nptValue = input._valueGet();
|
|
|
+ $input.removeClass('focus.inputmask');
|
|
|
+ if (nptValue != undoBuffer) {
|
|
|
+ $input.change();
|
|
|
}
|
|
|
if (opts.clearMaskOnLostFocus) {
|
|
|
- if (_val.call(input) == _buffer.join(''))
|
|
|
- _val.call(input, '');
|
|
|
+ if (nptValue == _buffer.join(''))
|
|
|
+ input._valueSet('');
|
|
|
else { //clearout optional tail of the mask
|
|
|
clearOptionalTail(input, buffer);
|
|
|
}
|
|
|
@@ -533,7 +534,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
}
|
|
|
if (opts.clearIncomplete) {
|
|
|
if (opts.clearMaskOnLostFocus)
|
|
|
- _val.call(input, '');
|
|
|
+ input._valueSet('');
|
|
|
else {
|
|
|
buffer = _buffer.slice();
|
|
|
writeBuffer(input, buffer);
|
|
|
@@ -541,22 +542,22 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
}
|
|
|
}
|
|
|
}).bind("focus.inputmask", function() {
|
|
|
- var input = $(this);
|
|
|
- input.addClass('focus.inputmask');
|
|
|
- undoBuffer = _val.call(input);
|
|
|
+ var $input = $(this), input = this;
|
|
|
+ $input.addClass('focus.inputmask');
|
|
|
+ undoBuffer = input._valueGet();
|
|
|
}).bind("mouseleave.inputmask", function() {
|
|
|
- var input = $(this);
|
|
|
+ var $input = $(this), input = this;
|
|
|
if (opts.clearMaskOnLostFocus) {
|
|
|
- if (!input.hasClass('focus.inputmask')) {
|
|
|
- if (_val.call(input) == _buffer.join(''))
|
|
|
- _val.call(input, '');
|
|
|
+ if (!$input.hasClass('focus.inputmask')) {
|
|
|
+ if (input._valueGet() == _buffer.join(''))
|
|
|
+ input._valueSet('');
|
|
|
else { //clearout optional tail of the mask
|
|
|
clearOptionalTail(input, buffer);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}).bind("click.inputmask", function() {
|
|
|
- var input = $(this);
|
|
|
+ var input = this;
|
|
|
setTimeout(function() {
|
|
|
var selectedCaret = caret(input);
|
|
|
if (selectedCaret.begin == selectedCaret.end) {
|
|
|
@@ -569,48 +570,47 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
}
|
|
|
}, 0);
|
|
|
}).bind('dblclick.inputmask', function() {
|
|
|
- var input = $(this);
|
|
|
+ var input = this;
|
|
|
setTimeout(function() {
|
|
|
caret(input, 0, lastPosition);
|
|
|
}, 0);
|
|
|
}).bind("keydown.inputmask", keydownEvent
|
|
|
).bind("keypress.inputmask", keypressEvent
|
|
|
).bind("keyup.inputmask", function(e) {
|
|
|
- var input = $(this);
|
|
|
+ var $input = $(this), input = this;
|
|
|
var k = e.keyCode;
|
|
|
- if (k == opts.keyCode.TAB && input.hasClass('focus.inputmask') && _val.call(input).length == 0) {
|
|
|
+ if (k == opts.keyCode.TAB && $input.hasClass('focus.inputmask') && input._valueGet().length == 0) {
|
|
|
buffer = _buffer.slice();
|
|
|
writeBuffer(input, buffer);
|
|
|
if (!isRTL) caret(input, 0);
|
|
|
}
|
|
|
}).bind(pasteEventName, function() {
|
|
|
- var input = $(this);
|
|
|
+ var input = this;
|
|
|
setTimeout(function() {
|
|
|
caret(input, checkVal(input, buffer, true));
|
|
|
}, 0);
|
|
|
}).bind('setvalue.inputmask', function() {
|
|
|
- var input = $(this);
|
|
|
+ var input = this;
|
|
|
setTimeout(function() {
|
|
|
- undoBuffer = _val.call(input);
|
|
|
+ undoBuffer = input._valueGet();
|
|
|
checkVal(input, buffer, true);
|
|
|
- if (_val.call(input) == _buffer.join(''))
|
|
|
- _val.call(input, '');
|
|
|
+ if (input._valueGet() == _buffer.join(''))
|
|
|
+ input._valueSet('');
|
|
|
}, 0);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
setTimeout(function() {
|
|
|
- lastPosition = checkVal(input, buffer, true);
|
|
|
- if (document.activeElement === input[0]) { //position the caret when in focus
|
|
|
- input.addClass('focus.inputmask');
|
|
|
- caret(input, lastPosition);
|
|
|
- } else if (opts.clearMaskOnLostFocus && _val.call(input) == _buffer.join(''))
|
|
|
- _val.call(input, '');
|
|
|
+ lastPosition = checkVal(el, buffer, true);
|
|
|
+ if (document.activeElement === el) { //position the caret when in focus
|
|
|
+ $input.addClass('focus.inputmask');
|
|
|
+ caret(el, lastPosition);
|
|
|
+ } else if (opts.clearMaskOnLostFocus && el._valueGet() == _buffer.join(''))
|
|
|
+ el._valueSet('');
|
|
|
}, 0);
|
|
|
|
|
|
//private functions
|
|
|
- function patchValueProperty(input) {
|
|
|
- var npt = input.jquery && input.length > 0 ? input[0] : input;
|
|
|
+ function patchValueProperty(npt) {
|
|
|
if (document.__lookupGetter__) {
|
|
|
if (!npt._valueGet) {
|
|
|
npt._valueGet = npt.__lookupGetter__("value");
|
|
|
@@ -713,7 +713,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
//Safari 5.1.x - modal dialog fires keypress twice workaround
|
|
|
skipKeyPressEvent = false;
|
|
|
|
|
|
- var input = $(this), k = e.keyCode, pos = caret(input);
|
|
|
+ var input = this, k = e.keyCode, pos = caret(input);
|
|
|
|
|
|
|
|
|
//backspace, delete, and escape get special treatment
|
|
|
@@ -736,7 +736,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
writeBuffer(input, buffer, beginPos);
|
|
|
}
|
|
|
}
|
|
|
- if (opts.oncleared && _val.call(input) == _buffer.join(''))
|
|
|
+ if (opts.oncleared && input._valueGet() == _buffer.join(''))
|
|
|
opts.oncleared.call(input);
|
|
|
|
|
|
return false;
|
|
|
@@ -752,7 +752,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
return false;
|
|
|
}
|
|
|
else if (k == opts.keyCode.ESCAPE) {//escape
|
|
|
- _val.call(input, undoBuffer);
|
|
|
+ input._valueSet(undoBuffer);
|
|
|
caret(input, 0, checkVal(input, buffer));
|
|
|
return false;
|
|
|
} else if (k == opts.keyCode.INSERT) {//insert
|
|
|
@@ -782,7 +782,7 @@ This plugin is based on the masked input plugin written by Josh Bush (digitalbus
|
|
|
if (skipKeyPressEvent) return false;
|
|
|
skipKeyPressEvent = true;
|
|
|
|
|
|
- var input = $(this);
|
|
|
+ var input = this;
|
|
|
|
|
|
e = e || window.event;
|
|
|
var k = e.which || e.charCode || e.keyCode;
|