HTML Help Forum HTML Help
Please Search for the answer to your question before asking it! Thanks.
 

Redirect to page based on specified set of input in form.
Post a Reply to this Topic Ask a New Question
Click here to go to the original topic
       HTML Help Forum -> Javascript
View previous topic :: View next topic  
Author Message
AlmightyMe



Joined: 28 Apr 2005
Posts: 1

Posted: Thu Apr 28, 2005 5:09 pm     Redirect to page based on specified set of input in form.  

I'm looking to create "validatezipcode.js" that will handle a specified set of zip codes that are posted from the form:

<form name="form" method="post" action="validatezipcode.js">
<div align="center"> Zip Code:
<input name="zip" type="text" id="zip" maxlength="5">
<input type="submit" name="Submit" value="Submit">
</div>
</form>

I want the user to enter a zip with 4 possible pages to load based on the zip code entered. If I enter 90210 or 90078 I want it to auto go to xyz.html. If 21146 another, and so on with multiple zips for each page. The fourth page should be loaded for any input not specified in the previous 3.

I haven't done any programming of any kind in like 2 years (I'm more the visual design type) and have been trying to get in the right direction for over 2 weeks now to no avail.

Any help would be most appreciated.
degsy



Joined: 23 Feb 2005
Posts: 2440
Location: North East, UK

Posted: Fri Apr 29, 2005 6:39 am      

For a simple basic script you can use IF/ELSE statements or a Switch statement.

Code:
<script>
function zipValidate(){
val = document.zipForm.zip.value;

   switch(val){
      case "90210":
         document.location = 'http://www.google.com';
         break;
      case "90078":
         document.location = 'http://www.yahoo.com';
         break;
      default:
         document.location = 'http://bbc.co.uk';
         break;
   }
}
      </script>
<form name="zipForm" id="zipForm" method="post" action="">
  <input name="zip" type="text" id="zip" maxlength="5" />
  <input name="zipButton" type="button" id="zipButton" value="Check" onclick="zipValidate()" />
</form>
torweb



Joined: 05 Aug 2005
Posts: 6

Posted: Fri Aug 05, 2005 1:12 pm     if/else statement  

I found your script and would like to use if for my site. My question, is it possible to add more than once "case" without repeating the whole line of code, as an example case "one, two, THREE, Three three.

Also can the the keyboard Enter key also duplicate the function of the screen Enter Button...

Thanks in advance for any help...

Tom
degsy



Joined: 23 Feb 2005
Posts: 2440
Location: North East, UK

Posted: Fri Aug 05, 2005 1:36 pm      

Yes you can combine cases

Code:
<script>
function zipValidate(){
val = document.zipForm.zip.value;

   switch(val){
      case "90210":
      case "90211":
         document.location = 'http://www.google.com';
         break;
      case "90078":
         document.location = 'http://www.yahoo.com';
         break;
      default:
         document.location = 'http://bbc.co.uk';
         break;
   }
}
      </script>
<form name="zipForm" id="zipForm" method="post" action="">
  <input name="zip" type="text" id="zip" maxlength="5" />
  <input name="zipButton" type="button" id="zipButton" value="Check" onclick="zipValidate()" />
</form>


The enter key should automatcially submit the form if the input box is focused.
torweb



Joined: 05 Aug 2005
Posts: 6

Posted: Fri Aug 05, 2005 1:42 pm     script  

Thank you very much for the reply...

Tom
skapapa



Joined: 04 Oct 2006
Posts: 3

Posted: Wed Oct 04, 2006 3:54 pm      

How would you go about using this script if you only wanted to validate the first few characters of the zip? For instance if it is 973XX it would take you to one page if it was 970XX it would take you to another page?

I am very new to this.

Thanks,
Perry
degsy



Joined: 23 Feb 2005
Posts: 2440
Location: North East, UK

Posted: Thu Oct 05, 2006 6:13 am      

Use substring()

http://javascriptkit.com/javatutors/string4.shtml
skapapa



Joined: 04 Oct 2006
Posts: 3

Posted: Thu Oct 05, 2006 6:34 am      

Thanks,

I know I must sound really dumb, but where in this code would you place the substring?
degsy



Joined: 23 Feb 2005
Posts: 2440
Location: North East, UK

Posted: Tue Oct 10, 2006 5:22 am      

There are examples in the link
mtague



Joined: 20 Dec 2007
Posts: 3

Posted: Thu Dec 20, 2007 10:04 am     using the substring  

I also have a project where I'm trying to redirect users based on the first 2 digits they enter into a form. I'm not familiar with programing in javascript and could really use the help to write out a statement using the substring as mentioned earlier in this post.

Here's what I've attempted, but I know it's probably terribly wrong:

Code: <script>
function zipValidate(){
val = document.zipForm.zip.value;substring(0,2)

   switch(val){
      case "01":
      case "02":
      case "03":
      case "04":
      case "05":
      case "06":
         document.location = 'http://www.google.com';
         break;
      default:
         document.location = 'http://www.yahoo.com';
         break;
   }
}
      </script>
     
     
<form name="zipForm" id="zipForm" method="post" action="">
  <input name="zip" type="text" id="zip" maxlength="5" />
  <input name="zipButton" type="button" id="zipButton" value="Check" onclick="zipValidate()" />
</form>

thanks
mtague



Joined: 20 Dec 2007
Posts: 3

Posted: Mon Dec 31, 2007 7:07 am     javascript redirect based on input text  

anyone know the answer to this one? I'm hoping to finish up a project that relies on this piece of code.

thanks!!

:D
 
 
DARFUR
HOSTING / DESIGN
MAKE MONEY

       HTML Help Forum -> Javascript
Page 1 of 1


Powered by phpBB Search Engine Indexer
Powered by phpBB 2.0.19 © 2001, 2002 phpBB Group