Compare two emails at registration time

Asked

Viewed 319 times

-2

I have a registration form. There are also two fields that are email and confirm email. How do I see if the two emails are equal in php and display an error message if they are not equal?

  • 2

    Would not be if ($email1 == $email2) { ... }? It was very vague your question. Please have as [Edit] and describe better what you need to do?

  • I don’t know how to ask this better, Anderson. I’m a layman, not if I program properly yet. What I want to know is if you can compare if two emails are right when it comes to registering without using javascript. Only using php or html. How could I be clearer? Can you help me please?

  • Now it’s even more confusing. You want to know if two emails are equal or if are right, in the sense that they are valid? You can improve the question by describing what your application represents and, above all, the form in question. For example: "I have a form that makes the registration of new users that has an email field, but I want that when the user enters a wrong email happens X, etc"

  • I just edited Anderson

1 answer

1


To retrieve the values typed in the form on the server side, with PHP, you can use the superglobal variable $_POST. It would be something like:

$email = $_POST["email"];
$confirmacaoDeEmail = $_POST["confirmacao_de_email"];

And to make sure they’re different:

if ($email != $confirmacaoDeEmail) {
    echo "Os e-mails informados não coincidem";
}

However, this does not guarantee that the values are, in fact, emails. The user could type the number 2 in the two fields, which would be equal and would not give error. To validate if they are valid emails, you can use the function filter_input:

$email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
$confirmacaoDeEmail = filter_input(INPUT_POST, "confirmacao_de_email", FILTER_VALIDATE_EMAIL);

// Verifica se são e-mails inválidos:
if (!$email || !$confirmacaoDeEmail) {
    echo "O e-mail não é um e-mail válido";
}

// Verifica se são diferentes:
if ($email != $confirmacaoDeEmail) {
    echo "Os e-mails informados não coincidem";
}

But reconsider using Javascript to do, also, this validation still on the client side, as you would avoid the client making an unnecessary request to the server. The sooner you inform the user of the error, the sooner it will be fixed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.