Javascript 表单验证不起作用
我有一个简单的表单验证脚本:
function validate() {
if(document.register.field.value == "") {
alert("Please fill in all fields");
return false;
} else {
return true;
}
}
使用 onSubmit 处理程序调用该函数,但单击“提交”时没有任何反应。它直接进入 PHP 脚本,而不是 javascript '拦截'它。有什么想法吗?
表单 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>
<input type='submit' name='submit' value='Register'>
</td>
</tr>
<tr>
<td>
* indicates a required field
</td>
</tr>
</table>
</form>
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>
<input type='submit' name='submit' value='Register'>
</td>
</tr>
<tr>
<td>
* indicates a required field
</td>
</tr>
</table>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的验证函数有语法错误。将方括号 ] 替换为右大括号 }:
Your validate function has a syntax error. Replace the square bracket ] with a right brace }: