consider there are two 2 input text types(text boxes) .say one have 12.34 and other 12.43.
Now we need to compare two values and print alert if first number is greater than second.
here fld_txt1 and fld_txt2 are the id's of the respective input types
var iValue1=document.getElementById("fld_txt1").value;
var iValue2= document.getElementById("fld_txt2").value;
if( iValue1>iValue2)
alert('number is greater');
But unfortunately this code will not work for all the cases
The javascript checks alwaysonly the numbers before "." ,so in this case 12 & 12
The solution for this problem is
var iValue1= parseFloat(document.getElementById("fld_txt1").value);
var iValue2= parseFloat(document.getElementById("fld_txt2").value);
if( iValue1>iValue2)
alert('number is greater');
This code works like a charm ,the magic here is typecasting.
No comments:
Post a Comment