www.webdeveloper.com
Results 1 to 10 of 10

Thread: form validation/redirect on submit

Hybrid View

  1. #1
    Join Date
    Apr 2007
    Posts
    2

    form validation/redirect on submit

    I am looking for some javascript code that will allow a user to input a six character code into a text box. If the code that is entered matches a predetermined code then I want the user to go to page a if it does not match the predetermined code then I want them to go to page b when they press the submit button. Also if no code is entered in the text box when they press the submit button I want them to go the page b.

    Can someone help me with this.

    Thanks in advance

  2. #2
    Join Date
    Oct 2006
    Location
    Ontario Canada
    Posts
    1,160
    Here is a way you can do it.....
    It not secure , but it works.


    copy and paste into a new document to try
    Code:
    <html>
    <head>
    
    <title>My Page</title>
    
    
    <script>
    
    var correctCode = "ABC123";
    
    
    function validateCode()
    {
    	var code = document.getElementById("codeTextBox").value;
    	
    	if (code == correctCode)
    	{
    		window.location.href = "http://www.google.com";
    	}
    	else
    	{
    		window.location.href = "http://www.yahoo.com";
    	}
    }
    </script>
    
    
    </head>
    
    <body>
      Enter Code: 
      <input type="text" name="codeTextBox" id="codeTextBox" />
      <input type="submit" name="Submit" value="Submit" onclick="validateCode()" />
    </body>
    </html>
    Drew

  3. #3
    Join Date
    Apr 2007
    Posts
    2

    Smile

    Drew

    It works perfectly just what I have been looking for. Thank You very much.

  4. #4
    Join Date
    May 2013
    Posts
    8
    Is there anyway to add multiple code options? So instead of just having the one code ABC123 - you could have GGG01, OOO03 and MTM02 as well? I tried just adding more var's but that didn't work. I also tried adding more if's statements and that didn't work either. Any help would be greatly appreciated. Thank you in advance.

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    Code:
    <script>
    var correctCode = ["ABC123","GGG01","OOO03","MTM02"];
    
    function validateCode(){
    var code = document.getElementById("codeTextBox").value;
    for(var i in correctCode){
    if(code === correctCode[i]){window.location.href = "http://www.google.com";break;}
    else{window.location.href = "http://www.yahoo.com";}
    }
    }
    </script>

  6. #6
    Join Date
    May 2013
    Posts
    8
    Thank you, that worked perfectly.

  7. #7
    Join Date
    May 2013
    Posts
    8

    Going a step further - multiple codes in 1 text box but with different landing URL's

    Okay, now let's say I wanted to do something like this. I have several codes that are all different and when an end user puts in GGG01 they get directed to page1.html

    if they put in this code OOO03 they get directed to page2.html

    if they put in this code MTM02 they get directed to page3.html

    All of these codes are going into that one field <input type="text" name="codeTextBox" id="codeTextBox" value="" size="10" />

    I tried many variations of the script but I cannot figure this out. Does anyone have an idea as to how I could get this to work?

  8. #8
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    try this
    Code:
    <script type="text/javascript">
    var correctCode = [
    ['GGG01','page1.html'],['OOO03','page2.html'],['MTM02','page3.html']
    ];
    
    function validateCode(){
    var code = document.getElementById("codeTextBox").value;
    for(var i in correctCode){
    if(code === correctCode[i][0]){window.location.href = correctCode[i][1];break;}
    else{continue;}
    }
    }
    </script>

  9. #9
    Join Date
    May 2013
    Posts
    8
    Awesome, that worked with some modification - I altered this line so that if they enter a wrong code they get taken to a fail.html page


    if(code === correctCode[i][0]){window.location.href = correctCode[i][1];break;}
    else{window.location.href = "Fail.html";}

    How do you guys know so much javascripting? I'd love to know more. I know a lot but this kind of stuff I didn't so please let me know, thanks.

  10. #10
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    for me it is just a hobby, i'm doing it in leasure, but there are many high skilled programmers over here - you can read their answers here and there almost in every thread. if you come here and help people trying to answer their questions, soon you'll notice that your own scripting became easier for you and it takes less time than before. i'm also trying to improve my poor english here learning it from english-speaking people ))
    Last edited by Padonak; 06-13-2013 at 04:58 PM.

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