用于表单验证的Javascript无法正常运行

时间:2014-01-06 02:38:59

标签: javascript php forms validation

在我的html文件的头部定义了脚本但是仍然不起作用是因为我的表单的动作导致了php文件?

<html>
    <head>
          <script>
         function validateForm()
          {
          var x=document.forms["myForm"]["food_name"].value;
           if (x==null || x=="")
            {
           alert("Food name must be filled out");
            return false;
             }
            </script>
<title> my title is going here </title>
</head>
<body>

 <form name="myForm"  action="http://xxxxxxxxx/~xxxxxxxx/file/phpfile.php" method="post" onsubmit="validateForm()">
        <b>foodName:</b> <input type="text" name="food_name"><br>
        <b>Food Type:</b> <input type="text" name="food_type"><br>
        <b>Total:</b> <input type="text" name="total"><br>
        <b>Available:</b> <input type="text" name="available"><br>
        <input type="submit" value="Submit">
        </form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

您的validateForm功能

中缺少大括号

答案 1 :(得分:1)

修复validateForm函数缺少的大括号}并更新onsubmit to this

onsubmit="return validateForm()"

另外,即使验证失败,它也会提交表格

检查http://jsfiddle.net/Thu69/

相关问题