Recent Articles
- Footer SlideUp For Any Website and Any AutoResponder, Started by shabbir in HTML/DHTML - JavaScript/VBScript
- How to Load Advertisements Asynchronously, Started by shabbir in HTML/DHTML - JavaScript/VBScript
- How to Have Double Color Border Elements?, Started by shabbir in HTML/DHTML - JavaScript/VBScript
- VB Script to Compare Two Excel Files with Multiple Tabs, Started by naimish in HTML/DHTML - JavaScript/VBScript
- Working on HTML Enities in Javascript, Started by pradeep in HTML/DHTML - JavaScript/VBScript
Similar Articles
- Form validation with PHP & JavaScript, Started by pradeep in PHP
- Extending JavaScript Arrays, Started by pradeep in HTML/DHTML - JavaScript/VBScript
- JavaScript Triggers, Started by pradeep in HTML/DHTML - JavaScript/VBScript
I've created a button to demonstrate the bug. The bug is that parseInt can return an incorrect value. For example, parseInt("08") results in 0 instead of 8. And parseInt("09") results in 0 instead of 9. The reason for this is because the zero in front is trying to tell the browser that this is an octal (base 8) number, and "08" and "09" are not valid octal numbers. The button below builds statements from parseInt("01") through parseInt("09") and shows what the resulting value is. But it also does parseFloat("01") through parseFloat("09"). This shows that the bug does not exist with parseFloat.
Keep in mind that this bug only happens when the value being checked is a string and only when the string starts with a leading zero. So that's why it is difficult to notice. But if you're dealing with a web page that has user input, there's nothing prevening the user from entering 08 for a number field. To be 100% confident that you won't see the bug, use one of these two techniques:
Code: JavaScript
parseInt(parseFloat(<my text value>))
parseInt(<my text value>, 10)
Code: JavaScript
function buildString() {
var ret = "";
for (var i=1; i<=9; i++) {
// Build a statement like parseInt("0?") where ? varies from "1" to "9"
fn = "parseInt(\"0" + i + "\")";
// Evaluate the statement to get the result
ret += fn + " = " + eval(fn) + "\n";
// Do the same thing, except with parseFloat instead of parseInt
fn = "parseFloat(\"0" + i + "\")";
ret += fn + " = " + eval(fn) + "\n";
// This time do parseInt but specify base 10.
fn = "parseInt(\"0" + i + "\", 10)";
ret += fn + " = " + eval(fn) + "\n\n";
}
return ret;
}










Yet to provide details about himself



Linear Mode

