Click to See Complete Forum and Search --> : a weird problem


Jen Song
08-11-2003, 09:51 AM
Hi,

I have the following JSP page with a very simple Javascript function to display number 1 to 10. It display 1 to 7 and 10 correctly, but for "08" and "09", it displays 0. Why doesn't the parstInt funciton like 08 and 09?

<html>

<head>

<SCRIPT TYPE="text/javascript">
<!--

function validateInput () {
alert(parseInt("01"));
alert(parseInt("02"));
alert(parseInt("03"));
alert(parseInt("04"));
alert(parseInt("05"));
alert(parseInt("06"));
alert(parseInt("07"));
alert(parseInt("08"));
alert(parseInt("09"));
alert(parseInt("10"));
return true;
}

//-->
</SCRIPT>

</head>
<body>

<form action="button.jsp">
<button onClick="return validateInput()"> Test!
</button>
</form>

</body>

</html>

Charles
08-11-2003, 09:56 AM
I would guess that you have typed the letter O and the diget 8.

Jen Song
08-11-2003, 09:59 AM
No, that's not the case. I've checked a lot of times.

DJRobThaMan
08-11-2003, 10:18 AM
Why not try this....


<html>

<head>

<SCRIPT TYPE="text/javascript">
<!--
var i = 1;
function validateInput () {
do{
alert(parseInt(i));
++i;
}
while(i < 11);
}

//-->
</SCRIPT>

</head>
<body>

<form action="button.jsp">
<button onClick="return validateInput()"> Test!
</button>
</form>

</body>

</html>

Jen Song
08-11-2003, 10:35 AM
Thanks. I'm sure this works. Actually I found this problem in my real project JSP code. The JSP code I posted above was just a simple test program that I wrote to check whether parseInt worked with 08 and 09. In my real code, I would need to get the integer out from a 2 character string. I can't use the your code instead. Thanks a lot though.

SlankenOgen
08-11-2003, 10:49 AM
The function takes 2 arguments: parseInt(n, radix);

It is defaulting to an octal radix so specify the radix as 10

parseInt(n, 10);