Forum Moderators: open

Message Too Old, No Replies

parseInt() problem

Tiny annoying problem....

         

tomda

9:34 am on Jun 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, I have the following javascript that check if the value is between 3 and 75.

if (parseInt(document.form1.form_disptopics.value)<"3") {return false;}
if (parseInt(document.form1.form_disptopics.value)>"75") {return false;}

I have added parseInt (after consulting my Javascript Bible) because I noticed that entering "0002" was returning "true".

But it seems that parseInt() is not what I am looking for since for example:
A/ "0076" return "true" (value after parseInt() being "62").
B/ "0762" return "true" (value after parseInt() being "498").

Any existing javascript function (similar to intval() in PHP) that return the absolute value of any number (Including numbers like "000025")

Thanks

jshanman

12:39 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



try this:

parseInt('000025',10);
parseInt('0076',10);
parseInt('0762',10);

Use "10" as the second argument and it forces parseInt to use a base 10 when attempting to parse the number.

- JS

visionmonster

9:23 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



parseInt is pretty powerful:

i believe the leading zero makes it default to base 8
parseInt("0076",10) = 76;
parseInt("0076",8) = 62;

binary: parseInt("0100",2) = 4

hexadecimal: parseInt("ff",16) = 255;

tomda

5:33 am on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks...
This seems to be the solution to my problem.
Much appreciated.

vincevincevince

5:42 am on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is worth remembering that a leading 0 is not just ignored in javascript in most functions. 011!= 11