JS表单验证不适用于简单的html表单

时间:2014-03-24 08:59:19

标签: javascript php html

我有一个简单的HTML表单,只有一个字段。表格如下 -

<table> 
    <form action="insert.php" method="post" name="discover" onSubmit="return discover()">
    <tr><td></td><td><input type="text"  name="name"></td></tr>                                                             
    <tr><td></td><td><br><br><p class="submit"><input type="submit" id="submit" name="commit" value="Register"></p><br><br><br></td></tr>
    </form>
</table>

验证js文件是 -

function discover() {
   var stu_name=document.forms["discover"]["name"].value;

   if (stu_name==null || stu_name=="" || stu_name.length<7) {
      alert("Please provide Your full name");
      return false;
   }  
}

我已正确包含验证js文件。但验证不起作用。 Jsfiddle

的链接

5 个答案:

答案 0 :(得分:1)

试试这个

<script type="text/javascript">
function check()
{
var stu_name=document.forms["discover"]["stu_name"].value;
if (stu_name==null || stu_name=="" || stu_name.length<7)
  {
  alert("Please provide Your full name");
  return false;
  }
}
</script>
<form action="" method="post" name="discover" id="discover" onsubmit="return check();">
<input type="text" name="stu_name" id="stu_name" value="" />
<input type="submit" id="sub" />
</form>

答案 1 :(得分:0)

在你的js文件中,

测试:

function discover()
{
var stu_name=document.forms["discover"]["name"].value;


if (stu_name==null || stu_name=="" || stu_name.length<7)
{
      alert("Please provide Your full name");
      return false;
   }
}

答案 2 :(得分:0)

这项工作

function discover()
{
var stu_name=document.getElementsByName("name");


if (stu_name==null || stu_name=="" || stu_name.length<7)
{
      alert("Please provide Your full name");
      return false;
   }
}

答案 3 :(得分:0)

您正在混合表单名称和函数名称,并且输入还需要一个缺少的ID。你忘了关闭这个功能。 试试这个

<script type="text/javascript">
function _discover()
{
var stu_name=document.forms["discover"]["stu_name"].value;


if (stu_name==null || stu_name=="" || stu_name.length<7)
  {
  alert("Please provide Your full name");
  return false;
  }
}


</script>

<table> 
    <form action="" method="post" name="discover" onSubmit="return _discover();">
    <tr><td></td><td><input type="text"  name="name" id="stu_name"> </td></tr>                                                             
    <tr><td></td><td><br><br><p class="submit"><input type="submit" id="submit" name="commit" value="Register"></p><br><br><br></td></tr>
    </form>
</table>

答案 4 :(得分:0)

除了函数名称与表单

同名外,您的代码中没有错误

点击此链接,这是工作代码

jsbin

function disc() {
   var stu_name=document.forms["discover"]["name"].value;

   if (stu_name==null || stu_name=="" || stu_name.length<7) {
      alert("Please provide Your full name");
      return false;
   }  
}



<table> 
    <form action="insert.php" method="post" name="discover" onSubmit="return disc()">
    <tr><td></td><td><input type="text"  name="name"></td></tr>                                                             
    <tr><td></td><td><br><br><p class="submit"><input type="submit" id="submit" name="commit" value="Register"></p><br><br><br></td></tr>
    </form>
</table>
相关问题