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
Your function returns
falseas soon as the first range is checked. Reverse that logic:return trueif 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.