Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need javascript checking zipcode for hawaii alaska, etc

Tags:

javascript

Is there an easy way to do something (in javascript) depending on the zipcode? To be more precise, a user would enter in a zipcode and if that zipcode is outside of the continental usa (like hawaii, alaska, puerto rico, etc.) then it would do something like hide the free shipping option. I know this can easily be done with the state field, but how about the zipcode field?

like image 819
Malachi Avatar asked Aug 15 '26 11:08

Malachi


2 Answers

The set of all zip codes for locations in the continental U.S. (excluding of course Alaska, Hawaii, and U.S. territories) can be defined as follows:

Zip code is greater than 00999 but less than 96200

OR

Zip code is equal to or greater than 97000 but less than 99500

function isInContinentalUSA(zipcode)
{
    if((zipcode>'00999' && zipcode <'96200') || (zipcode>='97000' && zipcode <'99500')) {
      return true;
    } else {
      return false;
    }
}

UPDATE:

Outlying US territories follow these rules so I updated code above to manage Puerto Rico

US Outlying territories
AP: 962** - 966**
PW/FM/MH/MP/GU: 969**
AS: 96799
PR: 006-009

Appropriated from Addoa: http://addoa.com/blog/how-restrict-free-shipping-magento

like image 130
Nathaniel Johnson Avatar answered Aug 18 '26 02:08

Nathaniel Johnson


The USPS provides a free API for getting location information from a zip code. See https://www.usps.com/business/web-tools-apis/address-information.htm

like image 35
Kyle Avatar answered Aug 18 '26 00:08

Kyle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!