简体   繁体   English

家庭作业的帮助,我在这里做错了什么? [Javascript,验证]

[英]Homework help, what am I doing wrong here? [Javascript, validation]

Trying to get this form to validate email using the function the professor said to use. 尝试使用教授所说的功能来获取此表单以验证电子邮件。 We cannot use jquery or any other way to handle this. 我们不能使用jquery或任何其他方式来处理这个问题。 He's very...specific...on how he wants things done. 他非常......具体......关于他如何完成任务。 Anyway, last week of a web design course and introduces javascript without much explanation. 无论如何,上周的网页设计课程并没有太多解释介绍javascript。

The function is simply validating email but I have no frickin clue on how to call the function properly (verify_email). 该功能只是验证电子邮件,但我没有关于如何正确调用该函数的线索(verify_email)。 I've found countless examples of how to do this other ways but I'm pretty sure he will take off points for not doing it his way. 我已经找到了无数的如何做其他方式的例子,但我很确定他会因为没有按照他的方式做到这一点而取得积分。 Frantically trying to format this on an edit... it was fine when I submitted. 疯狂地尝试在编辑时格式化...当我提交时它很好。

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

try this 试试这个

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

Try using 尝试使用

 <script type="text/javascript">
  function verify_email (email_val)
  {
    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>

在页面正文中,您需要使用电子邮件的输入注册该功能。

<input type="text" name="email" onchange="verify_email()" />

This might be the problem: 这可能是问题所在:

function verify_email(email_val)
{
  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;
  }

  return true;
}

It will always return true . 它总是会返回true Also, search doesn't handle Regex. 此外, search不处理正则表达式。 You need to run the string onto the regex. 您需要将字符串运行正则表达式。 This code might work: 此代码可能有效:

function verify_email(email_val)
{
  var regex = /^[A-Z0-9_%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

  if (regex.exec(email_val) == -1)
  {
    alert("Email is not valid");
    return false;
  } else {
    return true;
  }
}

Also, see the comment Matt Phillips posted: Homework help, what am I doing wrong here? 另外,请参阅Matt Phillips发表的评论: 作业帮助,我在这里做错了什么? [Javascript, validation] . [Javascript,验证]

Also, verify_email(email) is not defined. 此外, verify_email(email) You should use verify_email(document.getElementById('email').value) . 您应该使用verify_email(document.getElementById('email').value)

Are you wanting to pass the string "email" to the email validation function? 您是否要将字符串“email”传递给电子邮件验证功能? Or do you want to actually check whatever is in the email input ? 或者您是否想要实际检查电子邮件input If you're just passing "email" to test, it needs to be in quotes ( ' ) for it to be passed correctly. 如果你只是通过“电子邮件”进行测试,它需要在引号( ' )中才能正确传递。

The javascript variable email is not defined anywhere so you are passing an undefined variable to the javascript function. javascript变量电子邮件未在任何地方定义,因此您将未定义的变量传递给javascript函数。 call the function like 调用函数就好

<input type="submit" value="Submit Comment" onclick="verify_email(document.getElementById('email').value);">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM