Strange string concat
Hi, this seems to be stupid but i find no explanation. I have this lines for testing the problem:
var cantidad;
cantidad = 0;
cantidad = cantidad + parseInt(document.forms[0].Cantidad.value);
cantidad = cantidad + parseInt(document.forms[0].Cantidad2.value);
the form can accept empty values and it seems that parseInt('') = NaN so, as I would do in VBscript or in Java, could fix the problem like this:
var cantidad;
cantidad = 0;
cantidad = cantidad + parseInt('0' + document.forms[0].Cantidad.value);
cantidad = cantidad + parseInt('0' + document.forms[0].Cantidad2.value);
alert(cantidad);
but it seems that a zero on the left does real diference!. For example, for a value in Cantidad2 of '1', I can put any value from '0' to '7' and even empty string. But after number '8' I don't really get the sum of Cantidad1 + 1. This is the arithmetics that is doing for example:
'' + 1 = 1
'4' + '1' = 5
'8' + '1' = 1
'200' + '1' = 129
Strange huh?... and how to fix this??
|