Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 表单未在提交时验证_Javascript_Html_Validation - Fatal编程技术网

Javascript 表单未在提交时验证

Javascript 表单未在提交时验证,javascript,html,validation,Javascript,Html,Validation,我正在尝试在提交时验证以下html表单的id字段。由于某些原因,它没有调用JavaScript验证并按原样提交表单。我是JavaScript新手,所以很难判断我犯了什么错误。任何帮助都将不胜感激 <!doctype html> <html> <head> <script type="text/javascript" src ="/javascripts/idcheck.js" > </script> </head> &l

我正在尝试在提交时验证以下html表单的id字段。由于某些原因,它没有调用JavaScript验证并按原样提交表单。我是JavaScript新手,所以很难判断我犯了什么错误。任何帮助都将不胜感激

<!doctype html>
<html>
<head>
<script type="text/javascript" src ="/javascripts/idcheck.js" >  </script>

</head>
<body>

<div class=page>
  <div class=metanav>
  <a href="<% menu_url %>">Menu</a>
  <% IF not session.logged_in %>
     <a href="<% login_url %>">login</a>                          
  <% ELSE %>
     <a href="<% logout_url %>">log out</a>
  <% END %>

 <% IF session.logged_in %>
    <form id ="frm1" method ="POST" onsubmit ="return idCheck(this)" action="<% identry_url %>">
 <% ELSE %>
     <a href="<% login_url %>">login</a>                                                        <% END %> 
</div

<% IF msg %>
    <div class=flash> <% msg %> </div>
 <% END %>

  <% IF flash.error %>
      <div class=error> <% flash.error %> </div>
  <% END %>
 <% content %>
</div>


<p>&nbsp;<b>RackID</b> <input type="text" id="rack" size =8>&nbsp; <b>Tech Initial</b>&nbsp;
<input type="text" id="tech" size=3> &nbsp; </p>

<p>&nbsp;<b>V&nbsp; </b> <input type="text" id="id1"  size =8>&nbsp;
<input type="text" id="id2" size =8>&nbsp;
<input type ="text" id="id3" size =8>&nbsp;
<input type="text" id="id4" size =8>&nbsp;</p>


<p>&nbsp;<b>W</b> 
<input type="text" id="id5" size =8>&nbsp;
<input type="text" id="id6" size =8>&nbsp;
<input type="text" id="id7" size =8>&nbsp;
<input type="text" id="id8" size =8>&nbsp;
</p>

<p>&nbsp;<b>X&nbsp;</b>
<input type="text" id="id9" size =8>&nbsp;
<input type="text" id="id10" size =8>&nbsp;  
<input type="text" id="id11" size =8>&nbsp;
<input type="text" id="id12" size =8>&nbsp;
</p>

<p>&nbsp;<b>Y&nbsp;</b>
<input type="text" id="id13" size =8>&nbsp;
<input type="text" id="id14" size =8>&nbsp;
<input type="text" id="id15" size =8>&nbsp;
<input type="text" id="id16" size =8>&nbsp;
</p>

<p>&nbsp;<b>Z&nbsp;</b>
<input type="text" id="id17" size =8>&nbsp;
<input type="text" id="id18" size =8>&nbsp;
<input type="text" id="id19" size =8>&nbsp;
<input type="text" id="id20" size =8>&nbsp;
</p>



<input type="Submit" value="CheckID" name ="submit">
&nbsp;

</form>

</body>
</html>


当表单在提交时有效时,您应该
返回true
,希望继续执行此操作。

验证函数的这一部分会立即跳出,因为它是错误的:

var flds =document.getElementById("frm1");

 //Start Validation Loop
for (i = 2; i < flds.length; i++) {

的MDN条目包含有关浏览器兼容性的说明,以及有关其功能的详细信息。

您的函数可能有错误。尝试
flds[i]
而不是
flds.element[i]
。感谢您的输入,我感觉它正在每个文本字段中循环。我试图实现的是从第三个文本字段开始,循环通过类型为“text”的每个元素进行验证。我怎么能做到这一点呢?@user2838042编辑了我的答案,加入了一个方法,你可以使用这个方法。如果你想在文本字段中循环,你需要直接选择这些字段。编辑了JS后,仍然可以在不进行验证的情况下提交
var flds =document.getElementById("frm1");

 //Start Validation Loop
for (i = 2; i < flds.length; i++) {
var flds = document.getElementById('frm1').querySelectorAll('input[type="text"]');

for(i = 2; i < flds.length; i++) {
    id = flds[i].value;
    ...