0

I am using the following condition in javascript to filter my options based on postcode.

if (parseInt(postcode, 10) > 99 && parseInt(postcode, 10) < 10000 && postcode != '') {
    // code goes here...
}

where postcode can be any Australian postcode. It works for all except some random postcodes like 0852/0840/4840. I am not sure whether it works for all postcodes I just checked for random postcodes.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146

1 Answers1

0

I just checked and it works absolutely fine for the postcodes you mentioned. Perhaps there are issues in other browsers - that I have not checked anything save or the latest Firefox.

Either way, please make sure the parseInt returns what you expect. You could do something like this:

var parsed = parseInt(postcode, 10);
if (parsed > 99 && parsed < 10000 && postcode != '') {
    // code goes here...
} else {
    console.log(postcode, parsed);
}

The above will print the bad values to the console and you will probably find out what's wrong. Maybe they return NaN? Maybe the input wasn't trimmed?

By the way, I would rework your condition. postcode != '' is a narrow case; what if you input 'abc'? All such values will be NaN after passing them through parseInt, so you could as well do something like this:

var parsed = parseInt(postcode, 10);
if (!!parsed && parsed > 99 && parsed < 10000) {
    // code goes here...
}
mingos
  • 23,778
  • 12
  • 70
  • 107
  • Thanks a lot for your help. I figured this out. However the issue was with my google api not with parseint(). Thanks anyways. – user3890489 Jul 31 '14 at 02:26