[RESOLVED] check if variables match?
i need to check if the contents of 2 textboxes match and i load the contents of these 2 into a variable
Code:
var pass = document.getElementById('password').value;
var cpass = document.getElementById('cpassword').value;
correct the code if its wrong, because i'm kinda new at javascript, my friend told me to stay away from it if it was possible xD
so what i need to do is check if those 2 textboxes value match, how to do this?
Re: check if variables match?
Code:
if(pass == cpass){
alert('they match.');
}
Re: check if variables match?
Just a more verbose example :)
Code:
window.onload = function(){
document.getElementById("myForm").onsubmit = function (){
var pass = document.getElementById('password').value;
var cpass = document.getElementById('cpassword').value;
if(pass == cpass){
alert("They're the same!");
}else{
alert("They're not the same.");
}
return false;
}
}
This assumes that you've given your form an id of "myForm", and that you want the comparison to be done when the person submits the form. You would need to alter this code because it always returns false to the form (so it will never submit).
Re: check if variables match?
thanks Kows and Samba i only needed the code for checking, the rest i would like to do my self, and so i did:
Code:
function checkpassword() {
var pass = document.getElementById('password').value;
var cpass = document.getElementById('cpassword').value;
if(pass == cpass){
document.getElementById('passresult').innerHTML = "Passwords match";
} else {
document.getElementById('passresult').innerHTML = "Passwords do not match";
}
}