Comparing two fields in a form in Javascript - Doesn't work
Can anyone tell me what I am doing wrong? my comparePasswords () doesn't work ...
<script>
function comparePasswords()
{
if (post-form.password.value != post-form.password2.value)
{
$('passwordShow').innerHTML = '';
}
else
{
$('passwordShow').innerHTML = 'Passwords do not match';
}
}
</script>
<form id="post-form" action="signup.php" method="post" >
<input type="password" id="password" name="password" onkeyup="testPassword(this.value);" maxlength="50" size="25" tabindex="102" value="">
<input type="password" id="password2" name="password2" onkeyup="comparePasswords();" maxlength="50" size="25" tabindex="104" value="">
</form>
+1
a source to share
3 answers
you need to use
document.getElementById('password').value
and
document.getElementById('password2').value
Also, your logic is wrong. The first conditional block will be triggered if the passwords do not match.
EDIT
You seem to be using jQuery, so just use
$('#password').val()
to get the value of the field.
You must also set innerHTML with jQuery (can also) with the method html()
+1
a source to share
The problem lies in how you access the form object.
post-form.password
For the javascript engine, this is the same as:
post - (form.password)
Also, you will need to give your form an attribute name
(not just an ID) to access it this way. If you want to access it by name, you should use something like this:
document['post-form'].password
Or by id:
document.getElementById("post-form");
+1
a source to share