简体   繁体   中英

Can someone tell me what am i doing wrong

I am trying to get this to be if your name is Bob then you are register if not Sorry you are not allowed access but i can not figure out what I am doing wrong can someone help me thanks.

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
         <head>
        <title></title>
        <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
           </head>
           <script type="text/javascript">
           function verify() {
            var name="Please enter your name: ";
          if (firstName=="Bob") {
          alert("Now registered");
          }
        else {
         window.alert("Sorry you aren't allowed acess.")
        return false;
           }           
       </script>
       <form name="myForm" action="#" method="post" enctype="text/plain">
        <input type="text" name="BOB">First Name<br>
        <input type="button" value="Submit" onclick="verify();">
        </form>
       </body>
        </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
              "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">
             <head>
            <title></title>
            <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
               </head>
               <script type="text/javascript">
               function verify() {
                var name="Please enter your name: ";
var firstName = document.getElementById('firstName').value;
              if (firstName=="Bob") {
              alert("Now registered");
    return true;
              }
            else {
             window.alert("Sorry you aren't allowed acess.")
            return false;
               } }          
           </script>
           <form name="myForm" action="#" method="post" onsubmit="return verify();" enctype="text/plain">
            <input id="firstName" type="text" name="BOB"/>First Name<br>
            <input type="button" value="Submit"/>
            </form>
           </body>
            </html>

you have to use onsubmit on form tag and it must return true or false

Note that you are missing the closing braces for the function, this code works:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
           </head>
           <script type="text/javascript">
           function verify() 
           {
                var name="Please enter your name: ";
                if (myForm.firstName.value=="Bob") 
                {
                    alert("Now registered");
                }
                else 
                {
                    alert("Sorry you aren't allowed acess.")
                    return false;
               }    
         }           
       </script>
<form name="myForm" action="#" method="post" enctype="text/plain">
<input type="text" name=firstName>First Name<br>
<input type="button" value="Submit" onclick="verify();">
</form>
</body>
</html>

Update your javascript like so:

      <script type="text/javascript">
       function verify() {
         var name="Please enter your name: ";
         if (document.myForm.firstName.value=="Bob") {
           alert("Now registered");
           return true;
         }
         else {
           window.alert("Sorry you aren't allowed acess.")
           return false;
         }
       }           
   </script>

Then update your HTML form to this:

   <form name="myForm" action="#" method="post" enctype="text/plain">
    <input type="text" name="firstName" value="">First Name<br>
    <input type="button" value="Submit" onclick="verify();">
   </form>

This should work.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
    </head>
    <body>
    <script type="text/javascript">
        function verify() {
            var firstName = document.getElementById('firstName').value;
            if (firstName == "Bob") {
                alert("Now registered");
                return true;
            } else {
                window.alert("Sorry you aren't allowed access.");
                return false;
            }
        }         
    </script>
    <form name="myForm" action="#" method="post" onsubmit="return verify();" enctype="text/plain">
        <input id="firstName" type="text" name="BOB"/>First Name<br>
        <input type="submit" value="Submit"/>
    </form>
    </body>
</html>

你的onsubmit应该在表单处理程序(“表单”的开放标记)上,而不是在按钮上。

Several issues:

  • You don't have a javascript variable firstName anywhere. The script probably stops there.
  • Your form markup for the first name field is strange (why are you naming the text box "BOB"? You should give it an ID.
  • You need to access the form element in javascript properly.
  • When submitting a form, it is better to use a submit input type and hookup the form onsubmit (in this regard the answer by @Pravat is correct, though not on the other points).
  • This line does nothing - var name="Please enter your name: ";

Firstly the Javascript does not know what firstname is

To fix this you need to do two things:

  • Use the HTML <input name="BOB" id="firstName" value="" /> . Note the id attribute, we'll use this to let the JS find the element we want to examine.

  • Then in Javascript we can find what the user has entered in the input using document.getElementById('firstName').val ue.

This should let you do your comparison.

To fix minor parts of your code, I believe you forgot to open your <body> tag

your also missing a } for your function

self-close your input and br tags

Try to use:

var firstName = document.getElementById('firstName').value;

and put missing } for verify function

看到这个... ,你还有一个}缺失...就在</ script>之前......缺少的}是关闭验证功能的那个。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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