-
-
Save chrdesigner/1f3a7277425dde797b13a1540a7839e1 to your computer and use it in GitHub Desktop.
Como Implementar Validações Avançadas em Formulários com Contact Form 7 e Bootstrap 5.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="mb-3"> | |
[text* cpf class:form-control placeholder "Digite seu CPF"] | |
</div> | |
<div class="mb-3"> | |
[email* email class:form-control id:email placeholder "Digite seu e-mail"] | |
</div> | |
<div class="mb-3"> | |
[text cep class:form-control id:cep placeholder "Digite seu CEP"] | |
</div> | |
<div class="mb-3"> | |
[text endereco class:form-control id:endereco readonly placeholder "Endereço"] | |
</div> | |
<div class="mb-3"> | |
[text cidade class:form-control id:cidade readonly placeholder "Cidade"] | |
</div> | |
<div class="mb-3"> | |
[text estado class:form-control id:estado readonly placeholder "Estado"] | |
</div> | |
<div class="text-center"> | |
[submit class:btn class:btn-primary "Enviar"] | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Validação personalizada para CPF | |
add_filter('wpcf7_validate_text', 'validar_campo_cpf', 10, 2); | |
add_filter('wpcf7_validate_text*', 'validar_campo_cpf', 10, 2); | |
function validar_campo_cpf($result, $tag) { | |
$tag_name = $tag['name']; | |
if ($tag_name === 'cpf') { | |
$cpf = isset($_POST[$tag_name]) ? sanitize_text_field($_POST[$tag_name]) : ''; | |
if (!validarCPF($cpf)) { | |
$result->invalidate($tag, "CPF inválido. Por favor, insira um CPF válido."); | |
} | |
} | |
return $result; | |
} | |
function validarCPF($cpf) { | |
$cpf = preg_replace('/[^0-9]/', '', $cpf); | |
if (strlen($cpf) != 11 || preg_match('/(\d)\1{10}/', $cpf)) { | |
return false; | |
} | |
for ($t = 9; $t < 11; $t++) { | |
for ($d = 0, $c = 0; $c < $t; $c++) { | |
$d += $cpf[$c] * (($t + 1) - $c); | |
} | |
$d = ((10 * $d) % 11) % 10; | |
if ($cpf[$c] != $d) { | |
return false; | |
} | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('wpcf7_validate_email*', 'validar_campo_email', 10, 2); | |
function validar_campo_email($result, $tag) { | |
$tag_name = $tag['name']; | |
if ($tag_name === 'email') { | |
// Obtém o valor do campo de e-mail | |
$email = isset($_POST[$tag_name]) ? sanitize_email($_POST[$tag_name]) : ''; | |
// Verifica se o e-mail está vazio ou inválido | |
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
$result->invalidate($tag, "Endereço de e-mail inválido. Insira um e-mail válido."); | |
return $result; | |
} | |
// Verifica o domínio do e-mail | |
$dominio = substr(strrchr($email, "@"), 1); // Extrai o domínio | |
if (!checkdnsrr($dominio, "MX")) { | |
$result->invalidate($tag, "O domínio do e-mail não é válido ou não existe."); | |
} | |
} | |
return $result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('wp_enqueue_scripts', 'carregar_scripts_cep'); | |
function carregar_scripts_cep() { | |
wp_enqueue_script('viacep-js', get_template_directory_uri() . '/assets/js/viacep.js', ['jquery'], null, true); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementById('cep').addEventListener('blur', function () { | |
const cep = this.value.replace(/\D/g, ''); | |
if (cep) { | |
fetch(`https://viacep.com.br/ws/${cep}/json/`) | |
.then(response => response.json()) | |
.then(data => { | |
if (!data.erro) { | |
document.getElementById('endereco').value = data.logradouro; | |
document.getElementById('cidade').value = data.localidade; | |
document.getElementById('estado').value = data.uf; | |
} else { | |
alert('CEP não encontrado.'); | |
} | |
}) | |
.catch(() => alert('Erro ao buscar o CEP.')); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function carregar_estilos_bootstrap() { | |
wp_enqueue_style('bootstrap-css', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css'); | |
wp_enqueue_script('bootstrap-js', 'https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js', [], null, true); | |
} | |
add_action('wp_enqueue_scripts', 'carregar_estilos_bootstrap'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment