Javascript表单验证UNDEFINED

时间:2015-11-25 16:29:52

标签: javascript php html

我正在编写一些PHP中包含以下javascript。问题是它不起作用,开发人员工具一直在说"' validate_entry'未定义"但我不知道自己在做什么

我的代码是:

<script type="text/javascript">
function validate_entry()
{
    var name=document.getElementById("submitted").value;
    var system=document.getElementById("system").value;
    var details=document.getElementById("description").value;
    if(name.trim() == "")
    {
        alert("The name you entered is too short");
        return false;
    }    
    if(name.length > 64)
    {
        alert("The name you entered is too long");
        return false;
    }
    if(system.trim() == ""
    {
        alert("System is not valid");
        return false;
    }
    if(details == "")
    {
        alert("Description cannot be empty");
        return false;
    }
}
</script>

我有以下表格:

<form action="index.php" method="POST" onsubmit="return validate_entry();">
<table>
<tr>
<td><label for="submitted">Issue Submitted By</label></td>
<td><input type="text" name="submitted" id="submitted" tabindex="<?php echo $header[0]++; ?>"></td>
</tr>
<tr>
<?php $systems = get_system_names($connection);
if($systems==FALSE)
{
    echo "<td></td>\n";
    echo "<td></td>\n";
}
else
{
    echo "<td><label for=\"system\">System Name</label></td>\n";
    echo "<td><select name=\"system\" id=\"system\" tabinex=\"". $header[0]++. "\">\n";
    echo "<option>Please Select</option>\n";
    foreach($systems as $system)
    {
        echo "<option value=\"{$system[0]}\">{$system[1]}</option>\n";
    }    
    echo "</select></td>\n";
}    
?>
</tr>
<tr>
<td><label for="description">Issue Description</label></td>
<td><textarea name="description" id="description" tabindex="<?php echo $header[0]++; ?>"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" id="submit" tabindex="<?php echo $header[0]++; ?>"></td>
</tr>
</table>
<?php echo "</form>\n";

1 个答案:

答案 0 :(得分:1)

  1. 确保JavaScript的位置在html代码之后
  2. 尝试使用浏览器控制台测试JavaScript功能
  3. 如果确实有效,请尝试提交并处理其他错误:)