var CGCMaskBehavior = function(val) { return val.replace(/\D/g, '').length <= 11 ? '000.000.000-009' : '00.000.000/0000-00'; } $('[name="cpfcnpj"]').mask(CGCMaskBehavior, { onKeyPress: function(val, e, field, options) { field.mask(CGCMaskBehavior.apply({}, arguments), options); } }); $('[name="cpfcnpj"]').on('blur', function() { let cpfcnpj = $(this).val().replace(/[^\d]+/g, '') let inputCpfCnpj = this inputCpfCnpj.setCustomValidity(''); if (cpfcnpj.length == 11) { let Soma = 0, Resto; let cpf = cpfcnpj if ( cpf == '' || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999" ) { alert('CPF Inválido'); inputCpfCnpj.setCustomValidity('CPF Inválido'); $('[name="cli_tipo"]').val('') return false } for (i = 1; i <= 9; i++) { Soma = Soma + parseInt(cpf.substring(i - 1, i)) * (11 - i); } Resto = Soma % 11; if ((Resto == 0) || (Resto == 1)) { Resto = 0; } else { Resto = 11 - Resto; } if (Resto != parseInt(cpf.substring(9, 10))) { alert('CPF Inválido'); inputCpfCnpj.setCustomValidity('CPF Inválido'); $('[name="cli_tipo"]').val('') return false } Soma = 0; for (i = 1; i <= 10; i++) { Soma = Soma + parseInt(cpf.substring(i - 1, i)) * (12 - i); } Resto = Soma % 11; if ((Resto == 0) || (Resto == 1)) { Resto = 0; } else { Resto = 11 - Resto; } if (Resto != parseInt(cpf.substring(10, 11))) { alert('CPF Inválido'); inputCpfCnpj.setCustomValidity('CPF Inválido'); $('[name="cli_tipo"]').val('') return false } $('[name="cli_tipo"]').val(1) } else if (cpfcnpj.length == 14) { let cnpj = cpfcnpj if ( cnpj == '' || cnpj == "00000000000000" || cnpj == "11111111111111" || cnpj == "22222222222222" || cnpj == "33333333333333" || cnpj == "44444444444444" || cnpj == "55555555555555" || cnpj == "66666666666666" || cnpj == "77777777777777" || cnpj == "88888888888888" || cnpj == "99999999999999" ) { alert('CNPJ Inválido'); inputCpfCnpj.setCustomValidity('CNPJ Inválido'); $('[name="cli_tipo"]').val('') return false } tamanho = cnpj.length - 2 numeros = cnpj.substring(0, tamanho); digitos = cnpj.substring(tamanho); soma = 0; pos = tamanho - 7; for (i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2) pos = 9; } resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; if (resultado != digitos.charAt(0)) { alert('CNPJ Inválido'); inputCpfCnpj.setCustomValidity('CNPJ Inválido'); $('[name="cli_tipo"]').val('') return false } tamanho = tamanho + 1; numeros = cnpj.substring(0, tamanho); soma = 0; pos = tamanho - 7; for (i = tamanho; i >= 1; i--) {
soma += numeros.charAt(tamanho - i) * pos--;
if (pos < 2) { pos = 9; } } resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; if (resultado != digitos.charAt(1)) { alert('CNPJ Inválido'); inputCpfCnpj.setCustomValidity('CNPJ Inválido'); $('[name="cli_tipo"]').val('') return false } $('[name="cli_tipo"]').val() } else { alert('CPF/CNPJ Inválido'); inputCpfCnpj.setCustomValidity('CPF/CNPJ Inválido'); $('[name="cli_tipo"]').val('') return false } }) var FoneMaskBehavior = function(val) { return val.replace(/\D/g, '').length <= 10 ? '(00)0000-00009' : '(00)00000-0000'; } let foneOptions = { onKeyPress: function(val, e, field, options) { field.mask( FoneMaskBehavior.apply({}, arguments), options ); } }; $('[name="telefone1"]').mask(FoneMaskBehavior, foneOptions); $('[name="telefone2"]').mask(FoneMaskBehavior, foneOptions); $('[name="telefone3"]').mask(FoneMaskBehavior, foneOptions); $('[name="cep"]').mask('00000-000'); $('[name="cep"]').on('input', function() { if (/[0-9]{5}-[0-9]{3}/.test($(this).val())) { $.ajax({ url: 'https://sistema.receitanet.net/api/novo/cep/99999' .replace(99999, $(this).val()), success: function(data) { $('[name="endereco"]').val(data.logradouro) $('[name="bairro"]').val(data.bairro) $('[name="cidade"]').val(data.cidade) $('[name="uf"]').val(data.uf) $('[name="numero"]').focus() } }) } })
