/*
* Copyright (c) 2007-2008 Josh Bush (digitalbush.com)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* GS - Significantly modified from original version!!!!
* Version: 1.1.4
* Release: 2008-07-29
*/
//Predefined character definitions
(function($) {
$.fn.mask = function(_mask, settings) {
settings = $.extend({ placeholder: "_", completed: null }, settings);
return this.each(function(){
var input = $(this);
var mask = input.attr("mask") || _mask;
var locked = new Array(mask.length);
var ignore = false; //Variable for ignoring control keys
var empty = $.map(mask.split(''), function(c, i) {
return (locked[i] = ($._RegExpCharMap[c] == null))
? c : settings.placeholder;
}).join('');
function getCaret() {
try {
if (input[0].setSelectionRange)
return { pos : input[0].selectionStart,
len : input[0].selectionEnd - input[0].selectionStart
};
if (document.selection && document.selection.createRange) {
var range = document.selection.createRange();
return { pos : 0 - range.duplicate().moveStart('character', -100000),
len : range.text.length
};
}
} catch (e) {
return { pos:0, len:0 };
};
};
function setCaret(begin) {
if(input[0].setSelectionRange)
input[0].setSelectionRange(begin, begin);
else if (input[0].createTextRange) {
var range = input[0].createTextRange();
range.collapse(true);
range.moveEnd('character', begin);
range.moveStart('character', begin);
range.select();
}
};
function blurEvent() {
checkVal(true);
input.change();
return true;
};
function keydownEvent(e) {
var pos = getCaret();
var k = e.keyCode;
ignore = (k < 48 && k != 32);
var buf = initBuffer(input.val());
if (k == 8) { //backspace
if (pos.len)
buf.splice(pos.pos, pos.len);
else if (pos.pos > 0)
buf.splice(--pos.pos, 1);
else
return false;
var s = writeBuffer(buf);
if($.browser.opera) {
//Opera won't let you cancel the backspace, so we'll let it backspace over a dummy character.
input.val(s.substring(0,pos.pos)+" "+s.substring(pos.pos));
setCaret(pos.pos + 1);
}
else
setCaret(pos.pos);
return false;
}
if ((!ignore && pos.len && !e.ctrlKey && !e.altKey) || k == 46) { //delete
buf.splice(pos.pos, Math.max(pos.len, 1));
writeBuffer(buf);
setCaret(seekNext(pos.pos - 1));
}
return k != 46;
};
function keypressEvent(e){
e = e || window.event;
if (ignore) {
ignore = false; //Fixes Mac FF bug on backspace
return (e.keyCode == 8) ? false : null;
}
var k = e.charCode || e.keyCode || e.which;
if (e.ctrlKey || e.altKey) //Ignore
return true;
if ((k>=41 && k<=122) || k == 32 || k > 186) { //typeable characters
var buf = initBuffer(input.val());
var p = seekNext(getCaret().pos - 1);
if (p < mask.length
&& new RegExp($._RegExpCharMap[mask.charAt(p)]).test(String.fromCharCode(k))) {
buf.splice(p, 0, String.fromCharCode(k));
writeBuffer(buf);
setCaret(p = seekNext(p));
if(settings.completed && p == mask.length)
settings.completed.call(input);
}
}
return false;
};
function writeBuffer(buf) {
var r = initBuffer(buf.join(''));
return input.val(r.join('')).val();
};
function initBuffer(test) {
//try to place chars where they belong
var pos = 0, buf = empty.split('');
for (var i = 0; i < mask.length; i++)
if(!locked[i])
for (; pos < test.length; pos++) {
var reChar = new RegExp($._RegExpCharMap[mask.charAt(i)]);
if (test.charAt(pos).match(reChar)) {
buf[i] = test.charAt(pos++);
break;
}
}
return buf;
}
function checkVal(isBlur){
var p = getCaret();
var s = writeBuffer(initBuffer(input.val()));
if (!isBlur)
setCaret(p.pos);
else if (empty == s)
input.val("");
};
function seekNext(pos){
while (++pos < mask.length)
if (!locked[pos])
return pos;
return mask.length;
};
input.bind("blur", blurEvent);
input.bind("keydown", keydownEvent);
input.bind("keypress", keypressEvent);
//Paste events for IE and Mozilla thanks to Kristinn Sigmundsson
if ($.browser.msie)
this.oncut = this.onpaste = function(){ setTimeout(checkVal, 0); };
else if ($.browser.mozilla)
this.addEventListener('input', checkVal, false);
checkVal(true); //Perform initial check for existing values
input.Disposable(function() {
if (input) {
input[0].onpaste = input[0].oncut = null;
if ($.browser.mozilla)
input[0].removeEventListener('input', checkVal, false);
}
input = null;
});
});
};
$.fn.NumInput = function() {
return this.each(function(){
var input = $(this);
function keypress(e) {
e = e || window.event;
var k = e.charCode||e.keyCode||e.which;
return (k < 48 && k != 32)
|| "0123456789,.-".indexOf(String.fromCharCode(k)) >= 0;
};
input.bind("keypress", keypress);
input.Disposable(function() {
input = null;
});
});
}
})(jQuery);