Validate password with confirm password

0

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

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];
    }
}
    
asked by anonymous 29.07.2016 / 02:38

0 answers