Javascript form validation not working
I have a simple form validation script:
function validate() {
if(document.register.field.value == "") {
alert("Please fill in all fields");
return false;
} else {
return true;
}
}
the function is called using the onSubmit handler, but nothing happens when submit is clicked. It goes directly to the PHP script instead of javascript 'intercepting' it. Any thoughts?
Form HTML:
<form action='register.php' name='register' method='POST' onSubmit='return validate();'>
<table>
<tr>
<td>
Your Full Name:
</td>
<td>
*<input class='req' type='text' name='fullname'>
</td>
</tr>
<tr>
<td>
Email Address:
</td>
<td>
*<input class='req' type='text' name='email'>
</td>
</tr>
<tr>
<td>
Home Phone Number:
</td>
<td>
*<input class='req' type='text' name='homephone'>
</td>
</tr>
<tr>
<td>
Cell Phone Number:
</td>
<td>
*<input class='req' type='text' name='cellphone'>
</td>
</tr>
<tr>
<td>
Address:
</td>
<td>
*<input class='req' type='text' name='address'>
</td>
</tr>
<tr>
<td>
&开发者_StackOverflow社区lt;input type='submit' name='submit' value='Register'>
</td>
</tr>
<tr>
<td>
* indicates a required field
</td>
</tr>
</table>
</form>
Your validate function has a syntax error. Replace the square bracket ] with a right brace }:
function validate() {
if(document.register.field.value == "") {
alert("Please fill in all fields");
return false;
} else {
return true;
}
}
精彩评论