7 Replies - 801 Views - Last Post: 05 November 2012 - 04:49 PM

#1 Redbird5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 07-July 12

How do I validate a zip code?

Posted 04 November 2012 - 08:47 PM

I am looking for a tutorial that would allow me to create a check on a zip code.

The syntax I'm thinking of is;
if your zip code is 62025 then go to this page.
What I'm wanting is an alert box that would ask for your zip code and then once the user enters their zip code it would take them to a page.
The problem is I'm not sure where I can find a tutorial that will help me out or what it's called exactly. If I knew the name of the process I would google it.

This post has been edited by Redbird5: 04 November 2012 - 08:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How do I validate a zip code?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4318
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: How do I validate a zip code?

Posted 04 November 2012 - 08:53 PM

You can search for "zip code api" if you are looking to check if a zip code actually exists. One website that has an API is http://daspecster.gi...astic/demo.html and as you will see they also include some javascript to show you how you can call it.

Another one is: http://www.zippopotam.us/

Their responses are in JSON (javascript notation) and can be used by your Javascript in further processing of the returned data. :)
Was This Post Helpful? 0
  • +
  • -

#3 Redbird5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 07-July 12

Re: How do I validate a zip code?

Posted 04 November 2012 - 09:01 PM

My idea is to have an alert box pop up immediately when the page loads and asks for your zip code. For example, something like this:

<script type="text/javascript">
<!--
var age = 15;
if( age > 18 ){
   document.write("<b>Qualifies for driving</b>");
}else{
   document.write("<b>Does not qualify for driving</b>");
}
//-->
</script>

Was This Post Helpful? 0
  • +
  • -

#4 IJET  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 93
  • View blog
  • Posts: 121
  • Joined: 14-October 12

Re: How do I validate a zip code?

Posted 04 November 2012 - 09:34 PM

Hello, Redbird. I believe the best way to go about doing it is storing the re-direct inside of a function that also calls a prompt box. Check here for how to deal with a popup box that will take a users input. Store the code inside a function and then call that function in the html using an onload call in the body tag. Example: <body onload="checkZip()">

The Javascript code for re-directing is something like window.location = "http://www.dreamincode.net";

edit: here's an example.

Javascript:

function checkZip()
{
var zip=prompt("Please enter a zip code");

if (zip == 62025)
  {
  window.location = "http://www.google.com"
  }
}


HTML:
<body onload="checkZip()">

This post has been edited by IJET: 04 November 2012 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 Redbird5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 07-July 12

Re: How do I validate a zip code?

Posted 04 November 2012 - 10:11 PM

<script>
function myFunction()
{
var x;

var name=prompt("Please enter your zip code","62025");

if (name!=null)
  {
  [b]x="Hello " + name + "! How are you today?";[/b]
  document.getElementById("demo").innerHTML=x;
  }
}
</script>


This is what I'm looking for:
This first part the user types in their zip code.
The second part would be the prompt to send you to another page.
The third and final part would send you to another page.

This post has been edited by Redbird5: 04 November 2012 - 10:18 PM

Was This Post Helpful? 0
  • +
  • -

#6 Redbird5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 07-July 12

Re: How do I validate a zip code?

Posted 05 November 2012 - 02:49 PM

Thanks IJet.

The next this is to add multiple pages and zip codes. It would be the same code right?
function checkZip()
{
	var zip=prompt("Please enter a zip code");
	
	if(zip == 62025)
	{
		window.location = "www.google.com"
	}
else(zip == 62526)
{
window.location = "www.yahoo.com"
}
}

Was This Post Helpful? 0
  • +
  • -

#7 Redbird5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 07-July 12

Re: How do I validate a zip code?

Posted 05 November 2012 - 04:18 PM

I ran into a problem. Every time that I click OK or Cancel on the pop up box it takes me to a page and I want it to go to the index page, if the user clicks cancel.
Was This Post Helpful? 0
  • +
  • -

#8 Redbird5  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 07-July 12

Re: How do I validate a zip code?

Posted 05 November 2012 - 04:49 PM

When I go back to the index page the pop up window keeps coming up. I only want it to come up once when the page loads.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1