最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can someone tell me what am i doing wrong - Stack Overflow

programmeradmin2浏览0评论

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"
          ".dtd">
          <html xmlns="">
         <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>

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/TR/xhtml1/DTD/xhtml1-strict.dtd">
          <html xmlns="http://www.w3/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>
Share Improve this question asked Mar 7, 2011 at 16:43 norris1023norris1023 6313 gold badges16 silver badges25 bronze badges 3
  • What do you expect to happen that doesn't? – Oded Commented Mar 7, 2011 at 16:45
  • It not poping up that says Now registered or Sorry you arent allowed access. – norris1023 Commented Mar 7, 2011 at 16:47
  • 1 Have you thought that anyone can view your page source and bypass your security. – Diodeus - James MacFarlane Commented Mar 7, 2011 at 16:49
Add a ment  | 

10 Answers 10

Reset to default 3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
              "http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">
              <html xmlns="http://www.w3/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/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/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>

This should work.

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/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>

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>

your onsubmit should be on the form handler (the open tag of the "form"), not on the button.

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').value.

This should let you do your parison.

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

see this..., you also have a } missing... just before the < /script> ... the missing } is the one that closes the verify function.

发布评论

评论列表(0)

  1. 暂无评论