www.webdeveloper.com
Results 1 to 4 of 4

Thread: 2 boxes must have correct word to go to webpage.

  1. #1
    Join Date
    Nov 2013
    Posts
    15

    2 boxes must have correct word to go to webpage.

    Hello!

    I have this code:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
      <form onSubmit="return checkAnswer();">
      <input id="answer" type="text" maxlength="55" class="box" autofocus />
      <input type="submit" class="submit" value="SUBMIT" />
      </form>
      <script>
      function checkAnswer(){
          var response = document.getElementById('answer').value;
          if (response == "correctanswer")
              location = 'right.html';
          else
              location = 'wrong.html';
          return false;
      }
      </script>
    </body>
    </html>
    If i enter the correct answer to one box, when i press Submit button go to page right.html if not it goes to wrong.html.

    I need to have 2 boxes. If both boxes have the correct words to go to page right.html if not it goes to wrong.html.

    Example:
    box 1 correct word is: "car"
    box 2 correct word is: "blue"

    If i type in box 1 "car" and in the box 2 "blue" go to page "right.html" when press Submit button.

    If i type in box 1 "car" and in the box 2 "red" go to page "wrong.html" when press Submit button.
    If i type in box 1 "bus" and in the box 2 "blue" go to page "wrong.html" when press Submit button.

    Thank you!

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259
    Here's a possibility...
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
     <form onSubmit="return checkAnswer();">
      <input id="answer1" type="text" maxlength="55" class="box" autofocus />
      <input id="answer2" type="text" maxlength="55" class="box" />
      <input type="submit" class="submit" value="SUBMIT" />
     </form>
    <script>
    // For: http://www.webdeveloper.com/forum/showthread.php?287015-2-boxes-must-have-correct-word-to-go-to-webpage
    
    // Put following line into external file for increased (ha!!!) security (haha!!!)
      correctanswer = 'bluecar';
    // Can change order like: 'carblue' or "BlueCar"
    //  depending upon needs (Note: case sensitive)
    
      function checkAnswer(){
          var response = document.getElementById('answer1').value+document.getElementById('answer2').value;
          if (response == correctanswer) { location = 'right.html'; return true; }
                                    else { location = 'wrong.html'; return false; }
      }
    </script>
    </body>
    </html>
    Happy T.Day!

  3. #3
    Join Date
    Nov 2013
    Posts
    15
    Quote Originally Posted by JMRKER View Post
    Here's a possibility...
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=utf-8 />
    <title>JS Bin</title>
    </head>
    <body>
     <form onSubmit="return checkAnswer();">
      <input id="answer1" type="text" maxlength="55" class="box" autofocus />
      <input id="answer2" type="text" maxlength="55" class="box" />
      <input type="submit" class="submit" value="SUBMIT" />
     </form>
    <script>
    // For: http://www.webdeveloper.com/forum/showthread.php?287015-2-boxes-must-have-correct-word-to-go-to-webpage
    
    // Put following line into external file for increased (ha!!!) security (haha!!!)
      correctanswer = 'bluecar';
    // Can change order like: 'carblue' or "BlueCar"
    //  depending upon needs (Note: case sensitive)
    
      function checkAnswer(){
          var response = document.getElementById('answer1').value+document.getElementById('answer2').value;
          if (response == correctanswer) { location = 'right.html'; return true; }
                                    else { location = 'wrong.html'; return false; }
      }
    </script>
    </body>
    </html>
    Happy T.Day!
    Ty but when i enter correct answer doesn't go to webpage 'right.html'.

    I just found this code and works great.
    Code:
    <html>
    <head>
    </head>
    
    <body>
    
    <form name="login">
    Username<input type="text" name="userid"/>
    Password<input type="password" name="pswrd"/>
    <input type="button" onclick="check(this.form)" value="Login"/>
    <input type="reset" value="Cancel"/>
    </form>
    
    <script language="javascript">
    function check(form)/*function to check userid & password*/
    {
     
     if(form.userid.value == "mar" && form.pswrd.value == "rosu")
      {
        window.open('right.html')
      }
     else
     {
       window.open('wrong.html')
      }
    }
    </script>
    </body>
    </html>

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259
    Works fine for me when correct words are used, but it's your choice as to what code you use.

    Good Luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles