View Full Version : passing string starting with a zero

03-22-2004, 06:54 PM
I have a var called caldate which is made up of a day and a month ie

0202 (2nd feb(yes i'm english ;) ))

the var is used to create a td id and then again as a param in a javascript call.

The id is fine. The rollover for the call shows the correct info in the status bar, but the receiving script changes it!!


month_date = 01
day = 1

caldate="0" + day + month_date;// to give 0101
document.write("<td id="+caldate+"><a href='javascript:hilite("+String(caldate)+",true);'>"+day+"</a></td>")

when an alert is placed in the hilite script it gives 65!!!!!
the 2nd jan gives 129. the 1st and 2nd of feb give 66 and 130 (this pattern continues for every month and every day up to the 9th)

Where am i going wrong?

Mr J
03-22-2004, 07:32 PM
I am not to sure here but I see that you are passing a string concatenated to variables as an argument, maybe this is where the problem lies

month_date = 01
day = 1

caldate="0" + day + month_date;// to give 0101

03-22-2004, 07:32 PM
How about:

caldate+="" + "0" + day + month_date;//

Or something like that..


03-22-2004, 07:35 PM
What about when the date is higher than the 9th? You'd then have a date as

Roy Sinclair
03-22-2004, 07:52 PM
The problem you're seeing is that the other function (which you didn't show) is where the bug is. You're using the parseInt function to parse that number and because the number has a leading "0" the parseInt function is interpreting the input number as Octal (0202 octal = decimal 130). You need to add the "radix" argument to your parseInt function to force it to treat all input numbers as decimal numbers.

So instead of myVar = parseInt(myString) use myVar = parseInt(myString,10)

Adding that additional parameter will fix your problem nicely.

Also please note that ID names cannot start with a number or be purely numeric and still be valid, the current crop of browsers may forgive this but there may come a point in time where you run into a browser that won't so you should plan on fixing that problem too.

03-23-2004, 10:59 AM
Roy - woh, never realised numbers were so complicated, I thought reg exp was confusing!!:o

Desided to look into the whole octal/radix thing and wish I hadn't! I've decided to get rid of the whole zero at the begining thing!

Cheers for your help, I think this is one for a rainy day...or week!

03-23-2004, 06:13 PM
Browsers support IDs beginnning with digits in scripting, but you'll get problems when trying to style such elements using CSS.

03-23-2004, 06:21 PM
cheers for the tip - doesnt make a difference in this case but i will remember it in the future. what about numbers converted to strings - will that work ok with css?

03-23-2004, 06:31 PM
Attributes are always strings, so that doesn't make any difference. The first character needs to be /[a-zA-Z_]/.

Roy Sinclair
03-23-2004, 08:06 PM
parseInt is useful but you do need to be aware that you should always add that "radix" parameter unless you're delibarately expecting to process input numbers that could be octal, hex binary or decimal depending on the format of the number.