/
var
/
www
/
html
/
cetesb
/
theme
/
cetesb
/
javascript
/
Upload File
HOME
function validarCPF(cpf) { var msgInvalido = "Número de CPF inválido, por favor, digite novamente."; var msgTamanho = "Número de CPF inválido, por favor, informe 11 digitos.\n(Ex: 00123456789)"; if(cpf.length == 0) return true; if(cpf.length > 0 && cpf.length < 11){ window.alert(msgTamanho); return false; } if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") { window.alert(msgInvalido); return false; } soma = 0; for (i = 0; i < 9; i++) soma += parseInt(cpf.charAt(i)) * (10 - i); resto = 11 - (soma % 11); if (resto == 10 || resto == 11) resto = 0; if (resto != parseInt(cpf.charAt(9))) { window.alert(msgInvalido); return false; } soma = 0; for (i = 0; i < 10; i++) soma += parseInt(cpf.charAt(i)) * (11 - i); resto = 11 - (soma % 11); if (resto == 10 || resto == 11) resto = 0; if (resto != parseInt(cpf.charAt(10))) { window.alert(msgInvalido); return false; } return true; } function remove(str, sub) { i = str.indexOf(sub); r = ""; if (i === -1) return str; r += str.substring(0, i) + remove(str.substring(i + sub.length), sub); return r; } function mascara(o, f) { v_obj = o; v_fun = f; setTimeout("execmascara()", 1); } function execmascara() { v_obj.value = v_fun(v_obj.value); } function cpf_mask(v) { v = v.replace(/\D/g, ""); if(v.length > 11) v = v.substring(0, 11); return v; } $(function () { $('#id_username').on({ blur: function () { if (!validarCPF(this.value)){ this.focus(); } }, keypress: function () { mascara(this, cpf_mask); } }); });