获取JS中函数的未定义值

时间:2013-02-08 10:00:47

标签: javascript html

将undefined作为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>

2 个答案:

答案 0 :(得分:1)

这是因为,函数在DOM加载之前加载。尝试在<script>之前移动</body>标记并确保其有效。

备注

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

    <!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

答案 1 :(得分:0)

将脚本标记放在结束body标记之前,因为您当前没有元素的引用

<body>
<script>
your code in here
</script>
</body>