I am building a registration form. I have two fields where a user can enter his email (one for email, the second is re-enter email). I am trying to validate the two fields to confirm they match. What I have done is that when a user moves to the password field, the onfocus will call the validate function and check if the two fields match. If there is an error, the error will display in another textfield. The problem is that the code is not working!!
Here is the code:
<html>
<head>
<script>
var fieldalias="Email address field"
function verify(element1, element2){
var passed=false
if (element1.value==''){
document.f1.emailerror.value='Fill out the first email field';
element1.focus()
}
else if (element2.value==''){
document.f1.emailerror.value='Fill out the second email field';
element2.focus()
}
else if (element1.value!=element2.value){
document.f1.emailerror.value='The two emails are not matching';
element1.select()
}
else
passed=true
return passed
}
</script>
</head>
<body>
<p>Username: <br/>
<input class="tb10" type="text" name="username" />
</p><br/>
<p>Email: <br/>
<input class="tb10" type="text" name="email1" />
</p>
<p>Re-Enter Email: <br/>
<input class="tb10" type="text" name="email2" />
<input id="emerror" type="text" readonly name="emailerror"/>
</p><br/>
<p>Password: <br/>
<input class="tb10" type="password" name="password1" onfocus="verify(this.email1,this.email2)";/>
</p>
</body>
</html>
onfocusin the password field? Why not just useonblurin the email fields? This way, the user isn't required to click into the password field for the code to be activated.