Click to See Complete Forum and Search --> : javascript bug??


Cluniac
07-30-2003, 03:05 PM
I have discovered a problem with one of my scripts, and I'm wondering if it is a problem with JavaScript itself.
i simplified my code to show directly the problem:

function validatedaterange() {
var begdate = document.getElementById("beg");
var enddate = document.getElementById("end");

//get the values for these 2 fields
var beg = begdate.value;
var end = enddate.value;

var beg1 = parseInt(beg.substr(0,2));
var end1 = parseInt(end.substr(0,2));

if(end1 > beg1){
return true;
}

else{
alert("The ending Date must be later than the beginning Date!");
return false;
}
}

For some reason, for variable "end1", whenever the substring being parsed is "08" or "09", it parses to: 0, and the function returns false. However, if "01", "02", etc. up to 7 is parsed, the number is returned, and the function returns true. 10-12 work fine.

Any suggestions as to why this might be happening?

Cluniac
07-30-2003, 03:24 PM
After doing some more research, I discovered that with the parseInt() function you can supply what base your numbers are in. Apparently when you numbers start with "0", like 08 or 09, the numbers are treated as octal and not decimal. In octal only the digits 0-7 are used, so that's why it didn't recognize the 8 and 9!

I needed to do this:

parseInt(beg1, 10);

to tell parseInt I want a decimal number.

SlankenOgen
07-30-2003, 04:41 PM
!!!!!!!!!!!!!!!!!!!!!?????????????????? Nice work. Numbers can be very contrary at times.