Javascript表单验证无效

时间:2011-05-22 16:48:09

标签: validation forms

我有一个简单的表单验证脚本:

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>

1 个答案:

答案 0 :(得分:0)

您的验证功能有语法错误。用右支撑替换方括号]:

function validate() {
    if(document.register.field.value == "") {
        alert("Please fill in all fields");
        return false;
    } else {
        return true;
    }
}