SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Manchester
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript/HTML form validator to redirect

    Hi there,

    I'm trying to run a piece of code that exclusively uses HTML with Javascript. The purpose of the page is for the user to input a number, and hit the submit button. Depending on the number they've inputted, they will be told if it's correct or not. If it's correct, they'd be redirected to a certain page. If it's incorrect, they'd be redirected to another page. (Note: I know this may seem like a silly idea for a page, but it's actually a certain section within a game.)

    The problem I am having is that, even though the appropriate alert box appears depending on the user's choice, I can't seem to get it to redirect to another page.

    Here is the code. As you can see it is pretty straightforward. The alert()'s get executed but not the window.location's.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function check()
    {
    var guess = document.formegg.fieldname.value;
    if (guess == "")
    {
    alert ("You must give an answer!")
    return 0;
    }
    else if (guess=="9")
    {
    alert ("Correct!")
    window.location = "http://www.yahoo.com";
    }
    else if (guess != "9")
    {
    alert ("Incorrect")
    window.location= "http://www.google.com";
    }
    }
    </script>
    </head>
    <body>
    <form name="formegg">
    <input type="text" name="fieldname">
    <input type="submit" name="B1" value="Submit" onClick = "check()">
    </form>
    </body>
    </html>
    Your help will be much, much appreciated. I'm working on a tight deadline, so I hope to hear back from someone soon.

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <html>
    <head>
    <script>
    function check(oForm)
    {
    	var guess = oForm.fieldname.value;
    
    	if (guess == "")
    	{
    		alert ("You must give an answer!");
    	}
    	else if (guess=="9")
    	{
    		alert ("Correct!");
    		window.location = "http://www.yahoo.com";
    	}
    	else
    	{
    		alert ("Incorrect");
    		window.location= "http://www.google.com";
    	}
    }
    </script>
    </head>
    <body>
    
    <form>
    	<input type="text" name="fieldname">
    	<input type="button" name="B1" value="Submit" onclick="check(this.form);">
    </form>
    
    </body>
    </html>

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Manchester
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're a star!

    Thanks!

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Manchester
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was just wondering...

    The code is working on my machine, and a few others. But it isn't on a certain friend's, who is running the same browser as I am (IE 6), with the latest Java runtime environment. None of the script seems to be executed on their machine - not even the alert prompts. Have you any ideas what may be the cause of the problem?

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code above is JavaScript and has nothing to do with Java. Maybe they have JavaScript disabled?

  6. #6
    SitePoint Zealot PixelPaul's Avatar
    Join Date
    Nov 2003
    Location
    Wisconsin, USA
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code did exactly what I was looking for, but I am unable to get it to work properly using an image as a button instead of the browser submit button. Is it possible to use an image as a submit button or am I doing something wrong?


Bookmarks

Posting Permissions

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