我正在尝试在提交时验证以下 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> <b>RackID</b> <input type="text" id="rack" size =8> <b>Tech Initial</b>
<input type="text" id="tech" size=3> </p>
<p> <b>V </b> <input type="text" id="id1" size =8>
<input type="text" id="id2" size =8>
<input type ="text" id="id3" size =8>
<input type="text" id="id4" size =8> </p>
<p> <b>W</b>
<input type="text" id="id5" size =8>
<input type="text" id="id6" size =8>
<input type="text" id="id7" size =8>
<input type="text" id="id8" size =8>
</p>
<p> <b>X </b>
<input type="text" id="id9" size =8>
<input type="text" id="id10" size =8>
<input type="text" id="id11" size =8>
<input type="text" id="id12" size =8>
</p>
<p> <b>Y </b>
<input type="text" id="id13" size =8>
<input type="text" id="id14" size =8>
<input type="text" id="id15" size =8>
<input type="text" id="id16" size =8>
</p>
<p> <b>Z </b>
<input type="text" id="id17" size =8>
<input type="text" id="id18" size =8>
<input type="text" id="id19" size =8>
<input type="text" id="id20" size =8>
</p>
<input type="Submit" value="CheckID" name ="submit">
</form>
</body>
</html>
这是我用于验证的javascript函数
function idCheck() {
"use strict";
var chkstring, prime, flds, i, id, idlen, stub, rem;
chkstring = "ABCDEFGHJKLMNPQRSTVWXYZ";
prime = chkstring.length;
var flds =document.getElementById("frm1").querySelectorAll('input[type="text"]').;
//Start Validation Loop
for (i = 2; i < flds.length; i++) {
id = "";
//get the value of the field
id = flds[i].value;
idlen = id.length;
if (idlen !== 8) {
//flds[i].style.backgroundColor = 'red';
alert("flds[i].value is not 8 charecters long");
return false;
}
stub = id.substr(0, 7);
if (stub === 0) {
flds[i].style.backgroundColor = 'red';
return false;
}
stub = stub - 1;
rem = stub % prime;
if (chkstring.substr(rem, 1) !== id.substr(7, 1)) {
flds[[i].style.backgroundColor = 'red';
return false;
}
return true;
}
}