Validation doesn't works

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
bharanidharanit
Forum Newbie
Posts: 15
Joined: Sun Jan 03, 2010 9:13 pm

Validation doesn't works

Post by bharanidharanit »

Hi,
I used the javascript coding for validate my forms. But this one not working. Whats wrong with this?

Code: Select all

<html>
<head>
<title>Register Page</title>
<script type="text/javascript" language="javascript">
function validate_register()
{
    alert('hi');
    var usrname = document.getElementById('regusrname').value;
    var usrpwd = document.getElementById('regusrpwd').value;
    var cusrpwd = document.getElementById('regcusrpwd').value;
    if (usrname=="")
    {
        document.getElementById('v1').innerHTML = "Enter Username";
        return false;
    }
    elseif (usrpwd=="")
    {
        document.getElementById('v2').innerHTML = "Enter Password";
        return false;
    }
    elseif (usrpwd != cusrpwd)
    {
        document.getElementById('v3').innerHTML = "Password Missmatch";
        return false;   
    }   
    else
    {
    return true;
    }
}
</script>
</head>
<body>
<table width="100%" border="1">
<form method="POST"   action="chkregister.php">
<tr>
<td>Enter Username:</td>
<td><input type="text" id="regusrname" name="regusrname" /></td>
<td id="v1">&nbsp;</td>
</tr>
<tr>
<td>Enter Password:</td>
<td><input type="text" id="regusrpwd" name="regusrpwd" /></td>
<td id="v2">&nbsp;</td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><input type="text" id="regcusrpwd" /></td>
<td id="v3">&nbsp;</td>
</tr>
<tr>
<td>Enter Email Address:</td>
<td><input type="text" id="email" name="email" /></td>
<td id="v4">&nbsp;</td>
</tr>
<tr>
<td>Enter Captcha Verification</td>
<td>
<?php
 
require_once('recaptchalib.php');
 
// Get a key from http://recaptcha.net/api/getkey
$publickey = "6LePbAoAAAAAALvKwRXXsD8fhSCCqR_yKTHq4wfQ";
$privatekey = "6LePbAoAAAAAAGG3kNPiaSUfiHx0mexGYwOueBrt";
 
# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;
 
# was there a reCAPTCHA response?
if ($_POST["recaptcha_response_field"]) {
        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["recaptcha_challenge_field"],
                                        $_POST["recaptcha_response_field"]);
 
        if ($resp->is_valid) {
                echo "You got it!";
        } else {
                # set the error code so that we can display it
                $error = $resp->error;
        }
}
echo recaptcha_get_html($publickey, $error);
?>
</td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="Register" onsubmit="validate_register()" /></td>
</tr>
</form>
</table>
</body>
<html>
 
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Validation doesn't works

Post by VladSun »

Try using onsubmit handler in the FORM tag, instead using it in the submit button tag.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply