0

将未定义为方法 validateHallticket 的返回类型请检查代码并进行相应修改,以便当我单击提交按钮时,我应该能够获得适当的返回类型。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>4Cubes Site</title>
    <script type="text/javascript">
      function validateForm(form) {
      document.writeln(validateNames(form["firstname"]));         
      document.writeln(validateHallticket(form["hallticket"])); // getting undefined value 
          if (validateNames(form["firstname"]) && validateHallticket(form["hallticket"])) {
              form.sub`enter code here`mit();
          }
          else {
              alert("Please Fill the required Fields");
          }
      }

      function validateNames(inputField) {
          Names_help = document.getElementById('lastname_help');
          if (inputField.value.length == 0) {
              if (Names_help!= null) {
                  Names_help.innerHTML = "Please Enter a validate Name";              
                  return false;
              }
          }
          else {
              Names_help.innerHTML = "";
              return true;
          }

      }

      function validateHallticket(inputField) {

          var regex = /^\d{2}K91A\d{4}$/;
          var rs = regex.test(inputField.value);
          hallticket_help = document.getElementById('hallticket_help');

          if (!regex.test(inputField.value)) {
              if (hallticket_help != null) {
                  hallticket_help.innerHTML = "Enter a valid hallticket";
                  return false;
              }
          }
          else {
              hallticket_help.innerHTML = "";
              return true;
          }

      }
    </script>
  </head>
  <body>
    <center>
      <font face="Arabic Transparent" size="6" color="Teal">4cUBeS College</font>
    </center>

    <br></br>

    <br></br>

    <form method="post" action="servlet.do" name="myform">
      HallTicket:
      <input type="text" name="hallticket" id="hallticket"
             onblur="validateHallticket(this)"></input>
      <span id="hallticket_help" style="color:Red; font-style:italic;"> </span>
      <br></br>
      FirstName:
      <input type="text" name="firstname" id="firstname"
             onblur="validateNames(this)"></input>
      <span id="firstname_help" style="color:Red; font-style:italic;"> </span>
      <br></br>
      LastName:
      <input type="text" name="lastname" id="lastname"
             onblur="validateNames(this)"></input>
      <span id="lastname_help" style="font-style:italic; color:Red;"> </span>

      <center>
        <input type="button" value="SUBMIT" onclick="validateForm(this.form);"></input>

      </center>
    </form>
  </body>
</html>
4

2 回答 2

1

这是因为,函数在 DOM 加载之前加载。尝试在<script>之前移动标签</body>,它可以工作。

笔记

  1. return false;无效时添加。
  2. 将处理程序添加到标记的onsubmit事件中。<form>

完整代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>4Cubes Site</title>
  </head>
  <body>
    <center>
      <font face="Arabic Transparent" size="6" color="Teal">4cUBeS College</font>
    </center>

    <br></br>

    <br></br>

    <form method="post" action="servlet.do" name="myform" onsubmit="return false;">
      HallTicket:
      <input type="text" name="hallticket" id="hallticket"
             onblur="validateHallticket(this)"></input>
      <span id="hallticket_help" style="color:Red; font-style:italic;"> </span>
      <br></br>
      FirstName:
      <input type="text" name="firstname" id="firstname"
             onblur="validateNames(this)"></input>
      <span id="firstname_help" style="color:Red; font-style:italic;"> </span>
      <br></br>
      LastName:
      <input type="text" name="lastname" id="lastname"
             onblur="validateNames(this)"></input>
      <span id="lastname_help" style="font-style:italic; color:Red;"> </span>

      <center>
        <input type="button" value="SUBMIT" onclick="return validateForm(this.form);"></input>

      </center>
    </form>
    <script type="text/javascript">
      function validateForm(form) {
      document.writeln(validateNames(form["firstname"]));         
      document.writeln(validateHallticket(form["hallticket"])); // getting undefined value 
          if (validateNames(form["firstname"]) && validateHallticket(form["hallticket"])) {
              form.submit();
          }
          else {
              alert("Please Fill the required Fields");
          }
        return false;
      }

      function validateNames(inputField) {
          Names_help = document.getElementById('lastname_help');
          if (inputField.value.length == 0) {
              if (Names_help!= null) {
                  Names_help.innerHTML = "Please Enter a validate Name";              
                  return false;
              }
          }
          else {
              Names_help.innerHTML = "";
              return true;
          }
        return false;
      }

      function validateHallticket(inputField) {

          var regex = /^\d{2}K91A\d{4}$/;
          var rs = regex.test(inputField.value);
          hallticket_help = document.getElementById('hallticket_help');

          if (!regex.test(inputField.value)) {
              if (hallticket_help != null) {
                  hallticket_help.innerHTML = "Enter a valid hallticket";
                  return false;
              }
          }
          else {
              hallticket_help.innerHTML = "";
              return true;
          }
          return false;
       }
    </script>
  </body>
</html>

小提琴:http: //jsbin.com/apuyaw/1

于 2013-02-08T10:04:37.263 回答
0

将您的脚本标签放在结束正文标签之前,因为您目前没有元素的引用

<body>
<script>
your code in here
</script>
</body>
于 2013-02-08T10:05:08.067 回答