Validate password with confirm password

Asked

Viewed 3,820 times

0

I am trying to validate my password more accurate that is identical to the password confirm field, I am validating in php and using ajax to show error if the input value is incorrect. I have tried to compare using === more so far nothing, any hint?

html:

   <tr>
     <td><input type="password" id="senha" name="senha" placeholder="Senha" onBlur="Validate('senha', document.getElementById('senha').value);"><br><span id="campo_senha" class="error"></span></td>
   </tr>
   <tr>
     <td><input type="password" id="confirmsenha" name="confirmsenha" placeholder="Confirme sua senha" onBlur="Validate('confirmsenha', document.getElementById('confirmsenha').value);"><br><span id="campo_confirmsenha" class="error"></span></td>
   </tr>

JS/Ajax

    function Validate(field, value){

if(window.XMLHttpRequest){
    var xmlhttp = new XMLHttpRequest();
} else if(window.ActiveXObject){
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
}

var urlget = "php/register.php?field="+field+"&value="+value;
var url = "php/register.php";
var params = "field="+field+"&value="+value;

xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlhttp.onreadystatechange = function(){
    if(xmlhttp.readyState === 1){
        document.getElementById('campo_' +field+ '').innerHTML = '<font color="green">Verificando...</font>';   
    }

    if(xmlhttp.readyState === 4 && xmlhttp.status === 200){
        var resposta = xmlhttp.responseText;
        document.getElementById('campo_'+ field + '').innerHTML = resposta;
    }
};

xmlhttp.send(params);
}

PHP

    $error = array(
    "Campo deve ser preenchido.",
    "Somente 20 caracteres são permitidos.",
    "Somente letras e espaços são permitidas.",
    "Formato de e-mail inválido.",
    "Mínimo de 3 caracteres.",
    "Somente 30 caracteres são permitidos.",
    "Mínimo de 5 caracteres.",
    "Somente letras e números são permitidos.",
    "Senhas precisam ser idênticas."
);

$field = $_POST['field'];
$value = $_POST['value'];
$idfield = $_POST['id'];
$nome = "";
$sobrenome = "";
$email = "";
$senha = "";
$confirmsenha = "";

// Senha
if($field === "senha"){
    $senha = $value;
    if($senha === ""){
        echo $error[0]; 
    } else if(strlen($senha) > 20){
        echo $error[1]; 
    } else if(strlen($senha) < 5){
        echo $error[6]; 
    } else if(!preg_match("/^[a-zA-Z0-9]*$/", $senha)){
        echo $error[7]; 
    }
}

// Confirma senha
if($field === "confirmsenha"){
    $confirmsenha = $value;
    if($confirmsenha === ""){
        echo $error[0]; 
    } else if(strlen($confirmsenha) > 20){
        echo $error[1]; 
    } else if(strlen($confirmsenha) < 5){
        echo $error[6]; 
    } else if(!preg_match("/^[a-zA-Z0-9]*$/", $confirmsenha)){
        echo $error[7]; 
    } else if(!$confirmsenha == $senha){
        echo $error[8];
    }
}
  • 1

    The comparison seems to be right, but the variable $senha is $senha = '', soon it will return error if the password is different from `` (nothing), which is the same error as the $error[0] that is triggered when you enter an empty die.

  • I understood what you meant, but I don’t know how to pull the value for $password, because of the function, thank you.

  • I have a question, what do you want to do is not compare the password with the password confirmation? In your line of code I only see if the password and password confirmation is in the parameters you set. You wouldn’t have something like this: if($password===$confirmapassword) { echo "correct passwords"; } Else { echo "passwords don’t match"; ;}

  • 1

    You don’t have to go all this way. Just compare both in javascript... and when you submit the form, validate all entries normally, including password and password confirmation

  • A question: The problem would not be the fact that php is server-side, I mean, from what I understand, you’ve released the parameters for php to confirm. only php has already been read right? da um echo in some php variable and see when it appears or if it appears.

  • @Daniel Omine is right, you better do the analysis before releasing for php.

Show 1 more comment
No answers

Browser other questions tagged

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