Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save evertonthepaula/a275d169b50857a5f90f2ae8141bb092 to your computer and use it in GitHub Desktop.
Save evertonthepaula/a275d169b50857a5f90f2ae8141bb092 to your computer and use it in GitHub Desktop.
Validador de formulário básico mas muito útil
function checkMandatory(idForm){
var cont = [];
jQuery(idForm+" [data-Mandatory]").each(function(index, el) {
cont.push( validaCampo( jQuery(this) ) );
});
for (var i = 0; i < cont.length; i++) {
if(!cont[i]) return false;
}
return true;
}
//SCRIPT VALIDAÇÃO DOS CAMPOS INPUT(vazio/email/cpf/captcha);
function validaCampo(campo){
var inputVal = campo.val();
var mtdDefine = campo.attr("data-Mandatory");
var tipo = campo.attr("type");
// VALIDA SE OS CAMPOS ESTÃO TODOS PREENCHIDOS
if(!inputVal){
campo.addClass("obrigatorio");
setTimeout(function() {campo.removeClass("obrigatorio");}, 5000);
return false;
}
// VALIDA E-MAIL
if(tipo == "email"){
if(!checkEmail(inputVal)){
campo.addClass("obrigatorio").val('').attr({placeholder: 'Por favor digite um e-mail válido.'});
return false;
}
}
// VALIDA CPF
if(mtdDefine === 'cpf'){
if(!checkCPF(inputVal)){
campo.addClass("obrigatorio").val('').attr({placeholder: 'Por favor digite um CPF válido.'});
return false;
}
}
// VALIDA CAPTCHA
if(mtdDefine === 'captcha'){
return checkCaptcha();
}
return true;
}
//SCRIPT VALIDAÇÃO CPF
function TestaCPF(strCPF) {
strCPF = strCPF.replace(/[^0-9]+/g,'');
var Soma = 0;
var Resto;
if (strCPF == '00000000000' || strCPF == '11111111111' || strCPF == '22222222222' || strCPF == '33333333333' || strCPF == '44444444444' || strCPF == '55555555555' || strCPF == '66666666666' || strCPF == '77777777777' || strCPF == '88888888888' || strCPF == '99999999999') return false;
for (i = 1; i <= 9; i++) Soma = Soma + parseInt(strCPF.substring(i - 1, i)) * (11 - i);
Resto = (Soma * 10) % 11;
if ((Resto == 10) || (Resto == 11)) Resto = 0;
if (Resto != parseInt(strCPF.substring(9, 10))) return false;
Soma = 0;
for (i = 1; i <= 10; i++) Soma = Soma + parseInt(strCPF.substring(i - 1, i)) * (12 - i);
Resto = (Soma * 10) % 11;
if ((Resto == 10) || (Resto == 11)) Resto = 0;
if (Resto != parseInt(strCPF.substring(10, 11))) return false;
return true;
}
//SCRIPT VALIDAÇÃO EMAIL
function checkEmail(inputVal){
var filtro = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
return filtro.test(inputVal);
}
//SCRIPT VALIDAÇÃO DE CAPTCHA
function checkCaptcha(){
var captchaInput = jQuery('input[name="captchaInput"]').val().toUpperCase();
var captchaInputHash = jQuery('input[name="captchaInputHash"]').val()
var hash = 5381;
if (captchaInput == undefined || captchaInput == null || captchaInput == ""){
jQuery('input[name="captchaInput"]').addClass("obrigatorio").val('');
return false;
}
for( var i = 0; i < captchaInput.length; i++) {
hash = ( (hash << 5) + hash ) + captchaInput.charCodeAt(i);
}
if (hash != captchaInputHash){
jQuery('input[name="captchaInput"]').addClass("obrigatorio").val('');
return false;
}
return true;
}
//RETIRA A MENSAGEM DE VALIDAÇÃO DO PHP NA TELA;
jQuery(document).ready(function(jQuery) { jQuery('.msg').delay(3500).hide('slow'); });
//SOBE A TELA ATÉ O INCIO DO FORMULARIO;
// jQuery('html, body').animate({ scrollTop: jQuery(idForm).offset().top}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment