Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript operators could not compare integers with zeros (eg. 10, 100, 1000 etc)?

Tags:

javascript

The javascript operators in my program could not identify integers that ends with all zeros. For example it can't identify 10, 100, 2000, 90000 etc. Which also means any number with trailing 0's can't be read. It could identify and compare 20001 and such.

Sample code:

if ((document.forms.frmApprovalTrainees.elements[TxtFldName].value) > 100 )
{
 alert("Please enter approved quantity less than the requested"); 
   return false;
}

The (document.forms.frmApprovalTrainees.elements[TxtFldName].value) is supposed to retrieve the input value for validation. In the case above the operator identifies the 100 as 1. Any idea how to solve this?

like image 959
Wen Ji Avatar asked Feb 01 '26 05:02

Wen Ji


1 Answers

Because an input's value is a string. Use parseInt(). Don't forget to use a decimal radix.

if (parseInt(document.forms.frmApprovalTrainees.elements[TxtFldName].value, 10) > 100)

The radix is there to make sure the string is parsed as a decimal number (and not octal or hexadecimal, as FFFF can be though of as a number as well).

like image 127
Madara's Ghost Avatar answered Feb 03 '26 17:02

Madara's Ghost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!