1
I have the code below, which accepts values in your inputs and then sends them to the database.
I want that when the user clicks submit, if Cpf is invalid (EX:111.111.111-11), it does not send any value to the database and shows an Alert on the screen indicating that nothing has been added, need an if that has as a condition the valid Cpf, only if the Cpf is valid the values will be sent
Here we have the inputs
<form action="Dados.php" method="POST" >
<table class="table table-striped">
<thead>
<tr>
<th>Nome</th>
<th>Cpf</th>
<th>Cep</th>
<th>Rua</th>
<th>Bairro</th>
<th>Cidade</th>
<th>Estado</th>
<th>Ibge</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" class="form-control input-sm" name="nom" required><br></td>
<td><input type="text" class="form-control input-sm" id="cpf" name="cpf" required><br></td>
<td><input type="text" class="form-control input-sm" id="cep" name="end" required><br></td>
<td><input name="rua" class="form-control input-sm" type="text" id="rua" required /><br /></td>
<td><input name="bairro" class="form-control input-sm" type="text" id="bairro" required/><br /></td>
<td><input name="cidade" class="form-control input-sm" type="text" id="cidade" required/><br /></td>
<td><input name="uf" class="form-control input-sm" type="text" id="uf" required/><br /></td>
<td><input name="ibge" class="form-control input-sm" type="text" id="ibge" required/><br /></td>
</tr>
</tbody>
</table>
<input type="submit" value="Criar Registro" onclick="function validaCPF()" class="btn btn-default"><input type="button" class="btn btn-warning" value="Voltar"
onclick="window.location='/Banco_de_dados/index.php';"/>
</form>
And here we have the code that sends the values to the bank
<body>
<?php
//declarando variaveis//
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'meubd';
$nome = $_POST['nom'];
$cpf = $_POST['cpf'];
$cep = $_POST['end'];
$rua = $_POST['rua'];
$bairro = $_POST['bairro'];
$cidade = $_POST['cidade'];
$uf = $_POST['uf'];
$ibge = $_POST['ibge'];
function validaCPF($cpf = null) {
// Verifica se um número foi informado
if(empty($cpf)) {
return false;
echo "Cpf";
}
// Elimina possivel mascara
$cpf = ereg_replace('[^0-9]', '', $cpf);
$cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);
// Verifica se o numero de digitos informados é igual a 11
if (strlen($cpf) != 11) {
return false;
}
// Verifica se nenhuma das sequências invalidas abaixo
// foi digitada. Caso afirmativo, retorna falso
else if ($cpf == '00000000000' ||
$cpf == '11111111111' ||
$cpf == '22222222222' ||
$cpf == '33333333333' ||
$cpf == '44444444444' ||
$cpf == '55555555555' ||
$cpf == '66666666666' ||
$cpf == '77777777777' ||
$cpf == '88888888888' ||
$cpf == '99999999999') {
return false;
// Calcula os digitos verificadores para verificar se o
// CPF é válido
} else {
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;
$sql = 'INSERT INTO pessoas VALUES ("'.$nome.'", "'.$cpf.'", "'.$cep.'" , "'.$rua.'", "'.$bairro.'", "'.$cidade.'", "'.$uf.'", "'.$ibge.'")';
//verificando se os valores foram inseridos//
if ($conn->query($sql) === TRUE) {
echo "<br>Os valores foram adicionados corretamente";
} else {
echo "<br>Error: " . $sql . "<br>" . $conn->error;
}
}
}
//conectando ao banco de dados//verificando conexão//
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Conecxão Falhou: " . $conn->connect_error);
}
echo "Conexao foi um sucesso";
//fechando conexão//
$conn ->close();
?>
</body>
As I mentioned before, there may be many errors in my code, so I ask for your help.
There is an API ready for this type of validations, just import from Maven, follow the [link][1] [1]: https://github.com/jereztech/validation-br-api
– J. Jerez