12 de junho de 2013 14:47:01 ART
Trabalhando com Máscaras no JavaScript
1º Crie um arquivo com o nome de mascaras.js e cole o seguinte script:
Código:
/* ******************************************************************************************
*
* Nome da Classe: Masked Text Input
* Versão: 1.0
* Criado por: Dário C. Oliveira
* Data: 12 Julho 2011
* Copyright : DCO Informática
*
********************************************************************************************
*
* Script para Geração de Máscara em campo de Texto
*
*/
(function($) {
var pasteEventName = ($.browser.msie ? 'paste' : 'input') + ".mask";
var iPhone = (window.orientation != undefined);
$.mask = {
//Caracteres Pré-Definidos Aceitos
definitions: {
'9': "[0-9]",
'a': "[A-Za-z]",
'*': "[A-Za-z0-9]"
},
dataName:"rawMaskFn"
};
$.fn.extend({
//Função auxiliar para o posicionamento dos Caracteres
caret: function(begin, end) {
if (this.length == 0) return;
if (typeof begin == 'number') {
end = (typeof end == 'number') ? end : begin;
return this.each(function() {
if (this.setSelectionRange) {
this.setSelectionRange(begin, end);
} else if (this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', begin);
range.select();
}
});
} else {
if (this[0].setSelectionRange) {
begin = this[0].selectionStart;
end = this[0].selectionEnd;
} else if (document.selection && document.selection.createRange) {
var range = document.selection.createRange();
begin = 0 - range.duplicate().moveStart('character', -100000);
end = begin + range.text.length;
}
return { begin: begin, end: end };
}
},
unmask: function() { return this.trigger("unmask"); },
mask: function(mask, settings) {
if (!mask && this.length > 0) {
var input = $(this[0]);
return input.data($.mask.dataName)();
}
settings = $.extend({
placeholder: "_",
completed: null
}, settings);
var defs = $.mask.definitions;
var tests = [];
var partialPosition = mask.length;
var firstNonMaskPos = null;
var len = mask.length;
$.each(mask.split(""), function(i, c) {
if (c == '?') {
len--;
partialPosition = i;
} else if (defs[c]) {
tests.push(new RegExp(defs[c]));
if(firstNonMaskPos==null)
firstNonMaskPos = tests.length - 1;
} else {
tests.push(null);
}
});
return this.trigger("unmask").each(function() {
var input = $(this);
var buffer = $.map(mask.split(""), function(c, i) { if (c != '?') return defs[c] ? settings.placeholder : c });
var focusText = input.val();
function seekNext(pos) {
while (++pos <= len && !tests[pos]);
return pos;
};
function seekPrev(pos) {
while (--pos >= 0 && !tests[pos]);
return pos;
};
function shiftL(begin,end) {
if(begin<0)
return;
for (var i = begin,j = seekNext(end); i < len; i++) {
if (tests) {
if (j < len && tests.test(buffer[j])) {
buffer = buffer[j];
buffer[j] = settings.placeholder;
} else
break;
j = seekNext(j);
}
}
writeBuffer();
input.caret(Math.max(firstNonMaskPos, begin));
};
function shiftR(pos) {
for (var i = pos, c = settings.placeholder; i < len; i++) {
if (tests) {
var j = seekNext(i);
var t = buffer;
buffer = c;
if (j < len && tests[j].test(t))
c = t;
else
break;
}
}
};
function keydownEvent(e) {
var k=e.which;
//backspace, delete, and escape get special treatment
if(k == 8 || k == 46 || (iPhone && k == 127)){
var pos = input.caret(),
begin = pos.begin,
end = pos.end;
if(end-begin==0){
begin=k!=46?seekPrev(begin):(end=seekNext(begin-1));
end=k==46?seekNext(end):end;
}
clearBuffer(begin, end);
shiftL(begin,end-1);
return false;
} else if (k == 27) {//escape
input.val(focusText);
input.caret(0, checkVal());
return false;
}
};
function keypressEvent(e) {
var k = e.which,
pos = input.caret();
if (e.ctrlKey || e.altKey || e.metaKey || k<32) {//Ignore
return true;
} else if (k) {
if(pos.end-pos.begin!=0){
clearBuffer(pos.begin, pos.end);
shiftL(pos.begin, pos.end-1);
}
var p = seekNext(pos.begin - 1);
if (p < len) {
var c = String.fromCharCode(k);
if (tests[p].test(c)) {
shiftR(p);
buffer[p] = c;
writeBuffer();
var next = seekNext(p);
input.caret(next);
if (settings.completed && next >= len)
settings.completed.call(input);
}
}
return false;
}
};
function clearBuffer(start, end) {
for (var i = start; i < end && i < len; i++) {
if (tests)
buffer = settings.placeholder;
}
};
function writeBuffer() { return input.val(buffer.join('')).val(); };
function checkVal(allow) {
//Função que tenta colocar os caracteres onde eles pertencem
var test = input.val();
var lastMatch = -1;
for (var i = 0, pos = 0; i < len; i++) {
if (tests) {
buffer = settings.placeholder;
while (pos++ < test.length) {
var c = test.charAt(pos - 1);
if (tests.test(c)) {
buffer = c;
lastMatch = i;
break;
}
}
if (pos > test.length)
break;
} else if (buffer == test.charAt(pos) && i!=partialPosition) {
pos++;
lastMatch = i;
}
}
if (!allow && lastMatch + 1 < partialPosition) {
input.val("");
clearBuffer(0, len);
} else if (allow || lastMatch + 1 >= partialPosition) {
writeBuffer();
if (!allow) input.val(input.val().substring(0, lastMatch + 1));
}
return (partialPosition ? i : firstNonMaskPos);
};
input.data($.mask.dataName,function(){
return $.map(buffer, function(c, i) {
return tests&&c!=settings.placeholder ? c : null;
}).join('');
})
if (!input.attr("readonly"))
input
.one("unmask", function() {
input
.unbind(".mask")
.removeData($.mask.dataName);
})
.bind("focus.mask", function() {
focusText = input.val();
var pos = checkVal();
writeBuffer();
var moveCaret=function(){
if (pos == mask.length)
input.caret(0, pos);
else
input.caret(pos);
};
($.browser.msie ? moveCaret:function(){setTimeout(moveCaret,0)})();
})
.bind("blur.mask", function() {
checkVal();
if (input.val() != focusText)
input.change();
})
.bind("keydown.mask", keydownEvent)
.bind("keypress.mask", keypressEvent)
.bind(pasteEventName, function() {
setTimeout(function() { input.caret(checkVal(true)); }, 0);
});
checkVal(); //Executa verificação inicial de valores existentes
});
}
});
})(jQuery);
2º Crie um arquivo com o nome de mask.js e cole o seguinte script:
Código:
//O Nome da Márcara deve ser aplicado ao campo de Texto no valor da "id".
jQuery(function($){
$.mask.definitions['~']='[+-]';
$('#MdataINI').mask('99/99/9999');
$('#MdataFIM').mask('99/99/9999');
$('#Mfone').mask('(99) 9999-9999');
$("#Mrg").mask(aa-99999999");
$("#Mcpf").mask("999-99-9999");
$("#Mcep").mask("99999-999");
});
3º Agora para usar basta chamar os scripts na sua página.
Código:
e colocar no id do campo de texto a ser mascarado com umas das mascaras criadas no mask.js.
Por exemplo: Para colocar o CPF no input text use:
Código:
Comentário sobre o Arquivo: jQuery Necessário
scripts.rar [33.05 KB]