Jump to content

Recommended Posts

I'm trying to make sure password fields match before submitting, and no matter what I have done here, it always says "Passwords do not match"

 

What am I doing wrong?

<script type="text/javascript">
function toggle_password(password) {
    if (window.XMLHttpRequest) {
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    handle = document.getElementById(password);
    var url = 'ajax1.php?';
    if(handle.value.length > 0) {
        var fullurl = url + 'do=check_password_exists&password=' + encodeURIComponent(handle.value);
        http.open("GET", fullurl, true);
        http.send(null);
        http.onreadystatechange = statechange_password;
    }else{
        document.getElementById('password_exists').innerHTML = '';
    }
}

function statechange_password() {
    if (http.readyState == 4) {
        var xmlObj = http.responseXML;
        var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;
        document.getElementById('password_exists').innerHTML = html;
    }
}
</script>

 

Now the ajax1.php page

$do = $_GET['do'];
switch($do) {
    case 'check_password_exists':
        if(get_magic_quotes_gpc()) {
            $pass =  $_GET['password'];
		$pass1 = $_GET['password1'];
        }else{
            $pass =  addslashes($_GET['password']);
		$pass1 = addslashes($_GET['password1']);
        }
        header('Content-Type: text/xml');
        header('Pragma: no-cache');
        echo '<?xml version="1.0" encoding="UTF-8"?>';
        echo '<result>';
        if($pass == $pass1) {
            echo 'Password match';
        }
	else {
		echo 'Passwords do not match';
	}
        echo '</result>';
    break;

    default:
        echo 'Error, invalid action';
    break;
}

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/133219-solved-password-validation/
Share on other sites

You were right.

I'm pretty sure this isn't the correct way because now all it shows is 'Passwords Match'

<script type="text/javascript">
function toggle_password(password) {
    if (window.XMLHttpRequest) {
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    handle = document.getElementById(password,password1);
    var url = 'ajax1.php?';
    if(handle.value.length > 0) {
        var fullurl = url + 'do=check_password_exists&password' + encodeURIComponent(handle.value);
	var fullurl = url + 'do=check_password_exists&password1' + encodeURIComponent(handle.value);
        http.open("GET", fullurl, true);
        http.send(null);
        http.onreadystatechange = statechange_password;
    }else{
        document.getElementById('password_exists').innerHTML = '';
    }
}

function statechange_password() {
    if (http.readyState == 4) {
        var xmlObj = http.responseXML;
        var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;
        document.getElementById('password_exists').innerHTML = html;

    }
}
</script>

 

Have any idea on how to fix this?

Just do

<script type="text/javascript">
function check() {
var pass1 = document.getElementById("pass1").value;
var pass2 = document.getElementById("pass2").value;
if(pass1 == pass2) {
	return true;
} else {
	return false;
}
}
</script>

 

then set id's for each password field

 

and.. onsubmit="check()" for your form

You were right.

I'm pretty sure this isn't the correct way because now all it shows is 'Passwords Match'

<script type="text/javascript">
function toggle_password(password) {
    if (window.XMLHttpRequest) {
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    handle = document.getElementById(password,password1);
    var url = 'ajax1.php?';
    if(handle.value.length > 0) {
        var fullurl = url + 'do=check_password_exists&password' + encodeURIComponent(handle.value);
	var fullurl = url + 'do=check_password_exists&password1' + encodeURIComponent(handle.value);
        http.open("GET", fullurl, true);
        http.send(null);
        http.onreadystatechange = statechange_password;
    }else{
        document.getElementById('password_exists').innerHTML = '';
    }
}

function statechange_password() {
    if (http.readyState == 4) {
        var xmlObj = http.responseXML;
        var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;
        document.getElementById('password_exists').innerHTML = html;

    }
}
</script>

 

Have any idea on how to fix this?

 

It looks like you're rushing your code without really thinking about what you're adding.  So, let's take a step back and look at this objectively.

 

You have two password fields.  You need to send each of them to the underlying PHP script.  The first step is to obtain the value from each field:

 

var pass = document.getElementById('password');
var passCheck = document.getElementById('password1');

 

Okay, now, you need to send them along:

 

if(pass.value.length > 0 && passCheck.value.length > 0)
{
   var fullUrl = url + 'do=check_password_exists&password=' + encodeURIComponent(pass.value) + '&password1=' + encodeURIComponent(passCheck.value);

   /* continue like normal from here */

}

 

That should be all you need to change.  Just remember to think about each component individually.  In your first attempt, you were only sending one password to the PHP script.  In your second attempt, you were sending the value from the first password field twice.

Thank you for that

 

Now I don't any kind of results

<script type="text/javascript">
function toggle_password(password) {
    if (window.XMLHttpRequest) {
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //handle = document.getElementById(password);
    
var pass = document.getElementById('password');
var passCheck = document.getElementById('password1');
var url = 'ajax1.php?';	
        if(pass.value.length > 0 && passCheck.value.length > 0)
		{	

   			var fullUrl = url + 'do=check_password_exists&password=' + encodeURIComponent(pass.value) + '&password1=' + encodeURIComponent(passCheck.value);
        http.open("GET", fullurl, true);
        http.send(null);
        http.onreadystatechange = statechange_password;

    }else{
        document.getElementById('password_exists').innerHTML = '';
    }
}

function statechange_password() {
    if (http.readyState == 4) {
        var xmlObj = http.responseXML;
        var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;
        document.getElementById('password_exists').innerHTML = html;
      
    }
}
</script>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.