作业帮助,我在这里做错了什么? [JavaScript,验证]

试图获得该表格以使用教授说的功能验证电子邮件.我们不能使用jquery或任何其他方式来处理此问题.他非常…具体…他要如何完成事情.无论如何,上周的网页设计课程并没有太多解释地介绍了javascript.

该功能仅是验证电子邮件,但是我对如何正确调用该功能没有任何提示(verify_email).我发现了无数种其他方法的示例,但是我很确定他会因为不按照自己的方式做而获益.疯狂地尝试在编辑时格式化它…当我提交时很好.

 <!DOCTYPE html>
 <html lang="en-US">
 <head>
   <title>Feedback</title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <link type="text/css" rel="stylesheet" href="media/css/webpageCSS.css"/>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery /1.4.4/jquery.min.js"></script>

  <script type="text/javascript">
  function verify_email ()
  {
      var email_val=document.getElementById("email").value;
  var regex = /^[A-Z0-9_%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
  if( email_val.search( regex ) == -1)
  {
  alert("Email is not valid");
  return false;
  }
  else
  {
  return true;
  }
  }
 </script>


 </head>
 <body class="sdd">

  <nav>
    <a href="Project4.html">Home</a>
      &nbsp;&nbsp;<a href="resume.html">Resume</a>
      &nbsp;&nbsp;<a href="classList.html">Class List</a>
      &nbsp;&nbsp;<a href="misc.html">Miscellaneous</a>
      &nbsp;&nbsp;<a href="comments.html">Feedback</a>
  </nav>
    <header>
  <h1 class="sd">Feedback Page</h1> 
  </header>
    <div id="wrapper">
        <div id="leftcolumn2">
        </div>
    <div id="rightcolumn2">
     <section>
     <br><br>
      Feedback Form:
    <form name="comform" method="post" action="http://webdevfoundations.net/scripts/formdemo.asp" onsubmit="return verify_email();">
        <table class="comtab">
            <tr>
                <td>*First Name: <input type="text" name="fname" id="fname"></td>
                <td>*Last Name: <input type="text" name="lname" id="flname"></td>
            </tr>
            <tr>
                <td id="com" colspan="2"><textarea cols="60" rows=5 name="comments" id="comments">Enter your feedback here</textarea></td>
            </tr>
            <tr>
                <td class="alignl" colspan="2">Email (optional): <input type="text" name="email" id="email"></td>

            </tr>
            <tr>
                <td class="alignl" colspan="2"><input type="submit" value="Submit Comment" ></td>
            </tr>

        </table>
    </form>
    </section>

    <footer class="footbot">
  &copy; 2010
    </footer>
    </div>
    </div>

解决方法:

尝试这个

  <!DOCTYPE html>
    <html lang="en-US">
 <head>
  <title>Feedback</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <link type="text/css" rel="stylesheet" href="media/css/webpageCSS.css"/>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

      <script type="text/javascript">
      function verify_email ()
      {
          var email_val=document.getElementById("email").value;
      var regex = /^[A-Z0-9_%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
      if( email_val.search( regex ) == -1)
      {
      alert("Email is not valid");
      return false;
      }
      else
      {
      return true;
      }
      }
     </script>


    </head>
    <body class="sdd">

  <nav>
    <a href="Project4.html">Home</a>
          &nbsp;&nbsp;<a href="resume.html">Resume</a>
          &nbsp;&nbsp;<a href="classList.html">Class List</a>
          &nbsp;&nbsp;<a href="misc.html">Miscellaneous</a>
          &nbsp;&nbsp;<a href="comments.html">Feedback</a>
  </nav>
        <header>
  <h1 class="sd">Feedback Page</h1> 
  </header>
        <div id="wrapper">
            <div id="leftcolumn2">
            </div>
        <div id="rightcolumn2">
        <section>
        <br><br>
        Feedback Form:
        <form name="comform" method="post" action="http://webdevfoundations.net/scripts/formdemo.asp" onsubmit="return verify_email();">
            <table class="comtab">
                <tr>
                    <td>*First Name: <input type="text" name="fname" id="fname"></td>
                    <td>*Last Name: <input type="text" name="lname" id="flname"></td>
                </tr>
                <tr>
                    <td id="com" colspan="2"><textarea cols="60" rows=5 name="comments" id="comments">Enter your feedback here</textarea></td>
                </tr>
                <tr>
                    <td class="alignl" colspan="2">Email (optional): <input type="text" name="email" id="email"></td>

                </tr>
                <tr>
                    <td class="alignl" colspan="2"><input type="submit" value="Submit Comment" ></td>
                </tr>

            </table>
        </form>
        </section>

        <footer class="footbot">
      &copy; 2010
        </footer>
        </div>
        </div>
 </body>
</html>
上一篇:C#-自定义验证器并指定消息类型


下一篇:php-Laravel请求验证在版本5.2中抛出HttpResponseException