Results 1 to 5 of 5

Thread: help with a check word form

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default help with a check word form

    Hi every one.
    I'm fairly new to javascript and I need some help with a script

    This is what i've found be mashing together some scripts from a few different sites.

    <HTML>
    <HEAD>

    <script type="text/javascript">
    function notEmpty(){
    var myTextField = document.getElementById('myText');
    if(myTextField.value != "fred")
    alert("You entered: " + myTextField.value);
    else
    var answer = alert("Correct")
    if (answer){

    }
    else{
    alert("Bye bye!")
    window.location = "http://www.google.com/";
    }
    }
    </script>

    </HEAD>
    <BODY>

    <input type='text' id='myText' />
    <input type='button' onclick='notEmpty()' value='Form Checker' />

    </BODY>
    </HTML>


    I would like it if when you enter random text it comes up with an elert saying what you just entered. And when you enter the special word.... fred ...... it says correct and then redirects you to another page, in this case google.

    It is nearly doing this however when you enter text that isn't fred it comes up with the alert like it's meant to, but then redirects you to google any way.


    Any help on this would be appreciated

  2. #2
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Actually I've edited it down a bit more

    <HTML>
    <HEAD>


    <script type="text/javascript">
    function notEmpty(){
    var myTextField = document.getElementById('myText');
    if(myTextField.value != "fred")
    alert("You entered: " + myTextField.value);

    else

    alert("Correct")
    window.location = "http://www.google.com/";

    }
    </script>


    </HEAD>
    <BODY>



    <input type='text' id='myText' />
    <input type='button' onclick='notEmpty()' value='Form Checker' />




    </BODY>
    </HTML>


    But I still get the same problem. Any help is appreciated.

  3. #3
    Join Date
    May 2011
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Its because you have line breaks in the code in the wrong locations.

    As a rule of thumb I always use {} when using loops or conditions. As it avoids this sort of issue.

    The below code will work.
    HTML Code:
    <HTML>
    <HEAD>
    	<script type="text/javascript">
    		function notEmpty(){
    		var myTextField = document.getElementById('myText');
    		if(myTextField.value != "fred"){
    			alert("You entered: " + myTextField.value);
    		}
    		else{
    			alert("Correct");
    			window.location = "http://www.google.com/";
    		}
    	}
    	</script>
    </HEAD>
    <BODY>
    	<input type='text' id='myText' />
    	<input type='button' onclick='notEmpty()' value='Form Checker' />
    </BODY>
    </HTML>

  4. #4
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Thanks!

  5. #5
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default Just 2 more questions

    Hi everyone,
    I've got two more questions.
    Is there a way to make the code either fred or george? I mean if the user entered fred, they would be redirected, but if they entered george they would also be redirected.

    Also is there a way to make it so if they enter fred, it directs them to one page, but if they enter george, it directs them to a different page.

    Thanks for any help

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
  •