www.webdeveloper.com
Results 1 to 3 of 3

Thread: Need Help Validating a Large set of numbers

  1. #1
    Join Date
    May 2011
    Posts
    3

    Question Need Help Validating a Large set of numbers

    Hi all,

    I was hoping to find some direction on this question.

    I have a project where when someone enters certain zip codes they will get redirected to another website. There are about 1206 zip codes that if someone enters they will get redirected. I was thinking about doing a very large conditional statement though that seemed very long and hard?

    Does any one have a better approach to this problem?

    Thanks!

  2. #2
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Yeah, a database!

    One way would to have AJAX calls to the server to fetch the URL to send the person to, then the use of a database will reduce the need for a large conditional in to a simple AJAX server side request.

  3. #3
    Join Date
    Feb 2006
    Posts
    2,927
    If you can group the codes as number ranges you can handle multiple instances with one redirect url.

    For example, this function returns a state code depending on the zip code, you can do something similar by defining the ranges to be redirected.


    Code:
    function stateFromZip(zip){
        zip= Number(zip);
        var A, statezips={
            AK:[99501, 99950], AL:[35004, 36925], AR:[71601, 72959], AR:[75502, 75502],
            AZ:[85001, 86556], CA:[90001, 96162], CO:[80001, 81658], CT:[6001, 6389],
            CT2:[6401, 6928], DC:[20001, 20039], DC2:[20042, 20599], DC3:[20799, 20799],
            DE:[19701, 19980], FL:[32004, 34997], GA:[30001, 31999], GA2:[39901, 39901],
            HI:[96701, 96898], IA:[50001, 52809], IA2:[68119, 68120], ID:[83201, 83876],
            IL:[60001, 62999], IN:[46001, 47997], KS:[66002, 67954], KY:[40003, 42788],
            LA:[70001, 71232], LA2:[71234, 71497], MA:[1001, 2791], MA2:[5501, 5544],
            MD:[20331, 20331], MD2:[20335, 20797], MD3:[20812, 21930], ME:[3901, 4992],
            MI:[48001, 49971], MN:[55001, 56763], MO:[63001, 65899], MS:[38601, 39776],
            MS2:[71233, 71233], MT:[59001, 59937], NC:[27006, 28909], ND:[58001, 58856],
            NE:[68001, 68118], NE2:[68122, 69367], NH:[3031, 3897], NJ:[7001, 8989],
            NM:[87001, 88441], NV:[88901, 89883], NY:[6390, 6390], NY2:[10001, 14975],
            OH:[43001, 45999], OK:[73001, 73199], OK2:[73401, 74966], OR:[97001, 97920],
            PA:[15001, 19640], RI:[2801, 2940], SC:[29001, 29948], SD:[57001, 57799],
            TN:[37010, 38589], TX:[73301, 73301], TX2:[75001, 75501], TX3:[75503, 79999],
            TX4:[88510, 88589], UT:[84001, 84784], VA:[20040, 20041], VA2:[20040, 20167],
            VA3:[20042, 20042], VA4:[22001, 24658], VT:[5001, 5495], VT2:[5601, 5907],
            WA:[98001, 99403], WI:[53001, 54990], WV:[24701, 26886], WY:[82001, 83128]
        }
        for(var z in statezips){
            A= statezips[z];
            if(statezips.hasOwnProperty(z) && zip> A[0] && zip< A[1]){
                return  z.substring(0, 2);
            }
        }
        return zip;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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