//expressió regular de dni, passem $_POST[dni] i el comparem amb una expressió regular i assignem el resultat a la variable $dni_matches
preg_match('/[0-9]{8}-[A-Z]{1}/',$_POST['dni'],$dni_matches, PREG_OFFSET_CAPTURE);
//expressió regular de nom, passem $_POST[nom] i el comparem amb una expressió regular i assignem el resultat a la variable $nom_matches
preg_match('/[a-zïüéíóúàèòñç\s]{1,35}/',$_POST['nom'],$nom_matches, PREG_OFFSET_CAPTURE);
var_dump($dni_matches);
var_dump($nom_matches);


if($dni_matches && $nom_matches){
    
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $link = mysqli_connect("localhost", "root", "P-11fl32fg14", "registrarse");

    $query = "INSERT INTO registrarse (dni,nom) VALUES ('$_POST[dni]','$_POST[nom]')";
    echo $query;
    mysqli_query($link, $query);

    mysqli_close($link);
}
elseif ($_POST['dni'] != preg_match('/[0-9]{8}-[A-Z]{1}/',$_POST['dni'],$dni_matches, PREG_OFFSET_CAPTURE)) {
    echo "Els dni ha de tenir 8 numeros de llarg un guió alt i una lletra exemple 43564748-B.";
}
elseif ($_POST['nom'] != preg_match('/[a-zïüéíóúàèòñç\s]{1,35}/',$_POST['nom'],$nom_matches, PREG_OFFSET_CAPTURE)){
    echo "Els noms ha d'estar en minúscules exemple albert.";
}

I can enter name with character especial @
the regular expression is not working properly I want just this a-zïüéíóúàèòñç\s

if ($_POST['dni'] != preg_match('/[0-9]{8}-[A-Z]{1}/',$_POST['dni'],$dni_matches, PREG_OFFSET_CAPTURE)) {
    echo "Els dni ha de tenir 8 numeros de llarg un guió alt i una lletra exemple 43564748-B.";
}
elseif ($_POST['nom'] != preg_match('/[a-zïüçéíóúàèòñá\s]{1,35}/',$_POST['nom'],$nom_matches, PREG_OFFSET_CAPTURE)){
    echo "Els nom ha d'estar en minúscules exemple albert.";
}
else {
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $link = mysqli_connect("localhost", "root", "P-11fl32fg14", "registrarse");

    $query = "INSERT INTO registrarse (dni,nom) VALUES ('$_POST[dni]','$_POST[nom]')";
    echo $query;
    mysqli_query($link, $query);

    mysqli_close($link);
}

    Per the official documentation for preg_match():

    preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or false on failure.

    Therefore, comparing the result to the string being tested makes no sense. Rather, you probably just want to test if the result is not equal to 1, instead of not equal to the string.

      Both of them. You're doing the same thing both times. It's wrong both times.

      You're also doing nothing with $dni_matches, making it and the PREG_OFFSET_CAPTURE flag redundant.

      (The expression is also wrong (your check on nom won't stop me deleting everything in the table, for example).)

        I change the order of the if the sintaxis is the same
        but I am a beginner with php and regular expressions
        the first regular expression work properly but the second not.

        if (!preg_match('/[0-9]{8}-[A-Z]{1}/',$dni)) {
            echo "Els dni ha de tenir 8 numeros de llarg un guió alt i una lletra exemple 43564748-B.";
        }
        //"
        elseif (!preg_match('/[a-z\sàèòéíóúñýáïüç·-]{1,35}/',$nom)){
            echo "Els nom ha d'estar en minúscules exemple albert.";
        }
        //"
        elseif (preg_match('/[0-9]{8}-[A-Z]{1}/',$dni) && preg_match('/[a-z\sàèòéíóúñýáïüç·-]{1,35}/',$nom)) {
            mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
             $link = mysqli_connect("localhost", "root", "P-11fl32fg14", "registrarse");
        
            /*$nom_cometes_simples = str_replace("'",'"', $_POST['nom']);
            echo $nom_cometes_simples;*/
            $query = "INSERT INTO registrarse (dni,nom) VALUES ('$_POST[dni]','$_POST[nom]')";
            echo $query;
            mysqli_query($link, $query);
        
            mysqli_close($link);
        }

          Regular expressions are pretty advanced. I note that your first regex just checks for the presence of 8 numbers, followed by a dash and a letter, but doesn't require that those are the only characters in $dni. For example:

          $dni = 'This giant string has a lot of stuff but then also has 01234567-A and then more stuff after it.';
          if (!preg_match('/[0-9]{8}-[A-Z]{1}/',$dni)) {
              echo "Els dni ha de tenir 8 numeros de llarg un guió alt i una lletra exemple 43564748-B.";
          } else {
              echo 'This string passed the test: ' . $dni . "\n";
          }

          If you run that code you see that the big long string passes the regex. You also need some ^ and $ anchor characters, to assert the string starts with this pattern and ends with it also, as well as the D modifier to make sure preclude any newline chars.

          $dni = '01234567-A';
          if (!preg_match('/^[0-9]{8}-[A-Z]{1}$/D',$dni)) {
              echo "Els dni ha de tenir 8 numeros de llarg un guió alt i una lletra exemple 43564748-B.";
          }

          Furthermore, your code uses mysqli_connect to connect to the database but then doesn't bother to check if the connection was successful before using $link. You've also got your root password posted in your code. You might want to remove that.

            Write a Reply...