Dave
Dave

Reputation: 11

Javascript Zip Code form for a delivery area?

I'm a total Javascript noob and will be taking a class next semester and learning it but in the mean time, I have a basic website Im doing for a local business. On it I need to make a form with just one input and a submit button that a potential customer can enter their zip code into and it will say whether or not they are in the delivery area. I have a list of about 30 zip codes.

Can someone point me in the right direction of where to learn this functionality quickly? I assume I just need to type those zips into some array or something like that and then I have the submit action check for a match or not. I understand the logic but I have no idea where to begin with the code as I am currently just a html/css static webpage guy.

Thanks alot in advance. Dave

Upvotes: 1

Views: 1819

Answers (1)

offex
offex

Reputation: 1885

To learn how to assemble your form correctly check out this web site. Then you can have your array of zip codes on the server and check if the submitted code is in that array. How you do that will depend on what server setup you have.

To do client-side validation, you could set it up and check in the JavaScript. It would be something like:

Form:

<input type="text" id="zipCode">
<input type="button" onclick="checkZipcode()">

JavaScript:

var zipcodes = [12345, 54321];
function checkZipcode() {
  var i, validCode = false;
  for(i = 0;i < zipcodes.length; i++) {
    if (zipcodes[i] == document.getElementById('zipCode').value) {
          validCode = true;
      }
  }

However, never rely solely on client-side validation. Make sure to also perform server-side validation.

Upvotes: 2

Related Questions