Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6053549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:03:09+00:00 2026-05-23T08:03:09+00:00

I’ve run into a problem in my validation script to check the Australian postcodes.

  • 0

I’ve run into a problem in my validation script to check the Australian postcodes.
It doesn’t seem to be incrementing through the multi-dimension array which contains the postcode values.

Here’s the function:

function validateAustralia(postcode, ranges) {

           for (var i = 0; i < ranges.length; i++) {
                     console.log(i);
//returns only 0, when it should return 0, 1, 2.
                     console.log("postcode: " + postcode + " " + "ranges: " + ranges);
//returns postcode: 2000 ranges: 200,299,2600,2618,2900,2920
                     console.log("ranges - low: " + ranges[2][0] + " " + "ranges - high: " + ranges[2][1]);
                     //returns ranges - low: 2900 ranges - high: 2920
                if (postcode >= ranges[i][0] && (postcode <= ranges[i][1])) {
                     valid = true;
                     //confirmation();
                     //break;
                } else {
                     inelegible();
                     return false;
                }
           }
     }

For New South Wales, for example

ranges = [ [1000, 2599], [2619, 2898], [2921, 2999] ];

it’s only returning 1000 and 2599 — that is ranges[0][0] and ranges[0][1]
so someone entering the postcode for Dubbo (which is in New South Wales) is ruled invalid, because its postcode — 2830 — isn’t between 1000 and 2599.

jQuery’s $.each() iterates over the first array correctly, but I’m not sure how to get the values from the second level array.

Edit:
OK, so it was a late night, and I’m blind.
kojiro has most of the answer below, and a friend here also pointed it out: I’m terminating the iteration after the first run through.
I moved that if else loop outside the iteration and just test if the postcode is within range. If is, it’s valid.
Then, if valid = true I call the confirmation function and everything else is good:

function validateAustralia(postcode, ranges) {
    for (var i = 0; i < ranges.length; i++) {
            console.log(i);
            // returns 0, 1, 2 ...
            console.log("postcode: " + postcode + " " + "ranges: " + ranges);
            // for Dubbo (2830), for example, returns postcode: 2830 ranges: 1000,2599,2619,2898,2921,2999
            console.log("ranges - low: " + ranges[i][0] + " " + "ranges - high: " + ranges[i][1]);
            // returns  ranges - low: 1000 ranges - high: 2599,
            //          ranges - low: 2619 ranges - high: 2898, ...


        if (postcode >= ranges[i][0] && (postcode <= ranges[i][1])) {
            valid = true;
        //  alert("valid =" + valid);
        } 
        if (valid === true) {
            confirmation();
            // all good
        } else {
            inelegible();
            // Sorry, mate
        }
    }
}

Because I’m new here, (long time listener, first time caller) I can’t answer my own question, but that’s basically it.

Here’s the HTML and the calling function for @nnnnnn and anyone else who wants to see:
The user chooses their state from a select

<select id="states" name="states">
     <option selected="" value="">Please choose ...</option>
        <optgroup label="Australia" id="australia">
        <option value="act">Australian Capital Territory </option>
        <option value="nsw">New South Wales </option>
      <!-- ...and so on for the rest of the states -->

and inputs their postcode into a textbox

<input id="postcode" name="postcode" type="text" maxlength="4" />

which I get thusly

postcode = $('#postcode').val();

and check against a range of postcode values

function checkAustralia(state, postcode, ranges) {  
    //      has to be in the range of values
        switch (state) {
            //Australian states
            //match the whole postcode
            //postcodes with a leading '0' are validated as whole numbers without the '0'
        case 'act':
            ranges = [ [200, 299], [2600, 2618], [2900, 2920] ];
            validateAustralia(postcode, ranges);
            break;
        case 'nsw':
            ranges = [ [1000, 2599], [2619, 2898], [2921, 2999] ];
            validateAustralia(postcode, ranges);
            break;
// ...and so on for the rest of the states
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:03:10+00:00Added an answer on May 23, 2026 at 8:03 am

    Your function returns false as soon as the first range is checked. Reverse that logic: return true if the value is in the range, but return false only if the loop is entirely exhausted.

    Also, your code doesn’t always explicitly return a value. That isn’t obviously a problem, but it could be related to the confusion here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've run into the problem with NHibernate proxy validation using Castle.Validator component. It's looks
I run into follow problem. Did I miss anything? Association.all().count() 1 Association.all().fetch(1) [Association(**{'server_url': u'server-url',
I've run into a problem where I make changes to a few JavaScript files
I've run into a problem where I'm getting two printouts of my /etc/motd file
I often run into the problem that I have one stream full of data
Has anyone else run into this problem before? I've got a method that calls
Has anyone run into this problem... In my layout.phtml I have: <head> <?= $this->headTitle('Control
Has anyone run into this problem? I have a collection of comments that I
I've run into a problem when using a one to many relationship. I want
I have run into this problem before but never quite solved it. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.