problems to validate with preg_match and filter_var

2

I have a problem trying to validate input with preg_match and filter_var it does not validate me. I append the code to see what I'm failing.

<?php
include_once "conexion/conexion.php";

if(isset($_POST['guardar'])){


$nombresyapellidos = $_POST['nombresyapellidos'];
$email = $_POST['email'];
$user = $_POST['user'];
$pass = $_POST['pass'];
$pass2 = $_POST['pass2'];


 if(strlen(trim($nombresyapellidos)) < 1 || strlen(trim($email)) < 1 || strlen(trim($user)) < 1 || strlen(trim($pass)) < 1 || strlen(trim($pass2)) < 1) 
 {
   $errMSG = "¡ Ups Aviso: No pueden haber campos vacios. !";
 }

else if($_POST['pass']!=$_POST['pass2']) 
{ 
  $errMSG = "¡ Ups Aviso: Las contraseñas deben coincidir. !";
} 

else if(strlen($pass) < 8)
{
  $errMSG = "¡ Ups Aviso: La contraseña debe tener al menos 6 caracteres. !";
}

else if(strlen($pass) > 16)
{
  $errMSG = "¡ Ups Aviso: La contraseña no puede tener más de 16 caracteres. !";
} 

else if (preg_match ("/^[a-z]+$/", $_POST['pass']))
{
  $errMSG = "¡ Ups Aviso: La contraseña1 debe tener al menos una letra minúscula. !";
}

else if(preg_match("/^[A-Z]+$/", $_POST['pass']))
{
  $errMSG = "¡ Ups Aviso: La contraseña2 debe tener al menos una letra mayúscula. !";
}

else if(preg_match("/^[0-9]+$/", $_POST['pass']))
{
  $errMSG = "¡ Ups Aviso: La contraseña3 debe tener al menos un caracter numérico. !"; 
}

else if(preg_match("/^[\W]+$/", $_POST['pass']))
{
  $errMSG = "¡ Ups Aviso: La contraseña4 debe tener al menos un caracter especial. !";  
}

else if (filter_var(($_POST['email']), FILTER_VALIDATE_EMAIL)) 
{
  $errMSG = "¡ Ups Aviso: Dirección de correo inválida. !";
}

else if(preg_match("/^[a-zA-Z ]+$/", $_POST['nombresyapellidos']))
{
  $errMSG = "Este campo solo acepta letras.";
}

else{ 
    //si no hay errores continuo
    
asked by yoclens 01.06.2017 в 05:57
source

1 answer

1

You are using preg_match upside down. According to the preg_match documentation :

  

preg_match () returns 1 if pattern matches the subject given, 0 otherwise, or FALSE if an error occurred.

Keeping that in mind, for example, in this condition you have:

if (preg_match ("/^[a-z]+$/", $_POST['pass']))
{
  $errMSG = "¡ Ups Aviso: La contraseña1 debe tener al menos una letra minúscula. !";
}

If the pattern in the password is met, that is, if there is at least one character in lowercase, then the error message will be displayed. When it should be the other way round: if the pattern is not met, it displays the error message.

And with filter_var the same thing happens. If you go to the documentation of that function , you will see that if something is returned it means that it is correct ... but in the condition that you have is considered incorrect:

if (filter_var(($_POST['email']), FILTER_VALIDATE_EMAIL)) 
{
  $errMSG = "¡ Ups Aviso: Dirección de correo inválida. !";
}

A quick solution would be to deny the result of those functions, or check if they are false .

    
answered by 01.06.2017 / 07:06
source