John (no URL provided) sent me a question based on some problems he was having with a javascript he had written. I've seen this happen with a few people now so I think a "tip" is in order. In javascript, ASP, and PHP(?) (and I believe other scripting and programming languages) – you can not have a number (ie. Integer, Float, etc.) with leading zeros. If you do – it will think it's something else (specifically a hexidecimal, or octal number.)
Here's a definition of hexidecimal and octal numbers I found here:
Hexadecimal and Octal numbers may also be represented in JavaScript. A hexadecimal number (base 16) is represented with a leading 0x, or 0X. The case of the X represents whether to use upper-case of lower-case letters for the hexadecimal number. Octal numbers are represented with a leading 0.
(don't ask me to explain hexadecimal or octal numbers more than that. I've never had to use them, and I don't know how I would. I only understand the textbook explanation you see above. LOL!)
If you have a number with leading zeros, and you need to KEEP the leading zeros, you need to treat it like a string. (i.e. put quotes around it).
So let's say you have a function that takes an ID number. Your ID number has leading zeros. (Lets say this function simply needs to take your number and forward you to a page with the ID number as the "name" of the page).
function fakeFunction(id) {
document.location= id + ".htm"
}
… some code …
<a href="#" onClick="fakeFunction(00123)">This link to page 00123 won't work</a>
but
<a href="#" onClick="fakeFunction('00123')">this one will</a>
If you want to see what it's reading (and this is a really good bugtesting tip in general) put in an alert. So make this the first line in your function:
alert(id)
In this example – the first link will pop up an alert that says "83" (I guess 00123 is the octal (?) equivalent to 83) the second will pop up the correct value; "00123".
Additionally – if you're populating a database – and the database is expecting a *NUMBER* you can't pass it a string. So if you've made your number with leading zeros into a string – you won't be able to put it into the database. If your "id" field is set to accept a number, this insert statment will give you errors:
INSERT into student (name, id) values ("Joe Schmo", "00123")
It needs to be this:
INSERT into student (name, id) values ("Joe Schmo", 123)
Yes, the leading zeros are gone. I suggest adding them in later (or changing your field type). You can easily add leading zeros to a number in php using the sprintf function like this
$aNumberThatsAString = sprintf("%05d"."id");
Breaking that down: %0 (pad with zeros) 5 (so that there are 5 "digits") d (this is an integer/decimal number)
Feel free to add to this or correct me in the comments. Some of the above may be incorrect assumptions…