i am comparing password and confirm password through java script.my code-
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(password,"<b>Error: </b>Password must be filled out!")==false)
{password.focus();return false;}
else if (validate_required(cnfpassword,"<b>Error: </b>Confirm Password must be filled out!")==false)
{cnfpassword.focus();return false;}
else if (document.getElementById('password').value != document.getElementById('cnfpassword').value)
{password.focus();Sexy.error("<b>Error: </b>Passwords entered are not same!");
password.value="";cnfpassword.value="";return false;}
}
validate_required() function is working fine, it is showing alert msg but password compare is not working. But the same code is working fine in some other page.I have written some php code to avoid page caching-
<?php
session_start();
session_cache_limiter('nocache');
header('Pragma: no-cache');
?>
what's the problem???
3 Answers 3
Strange code !
What does the
with (thisform)
do ? Does it allow to have password and cnfpassword in the current scope ?
If so, why don't you do :
else if (password.value != cnfpassword.value)
?
Apart from this, I suggest you to validate the form ALSO in PHP.
2 Comments
with is generally agreed to be a JavaScript mistake that should never be used. Unfortunately, the bloody awful example code on w3schools uses it, which is why we keep seeing this ugly and completely pointless validate_required function popping up again and again. Die, w3schools, die.It probably has to do with the values themselves; do you have a javascript debugger to help you step through and see where the error is happening? If you're using Firefox, I recommend the addon Firebug.
Comments
make sure the form input id's are correct (password and cnfpassword). also if they are, you could try changing
if (document.getElementById('password').value != document.getElementById('cnfpassword').value)
to
if (password.value != cnfpassword.value)
!symbol is used to not the boolean result of your function and turn false to true.!validate_required(password,"<b>Error: </b>Password must be filled out!")