[thelist] javascript comparison
Jason Handby
jasonh at corestar.co.uk
Wed Oct 29 12:50:08 CST 2003
Hi Rob,
> My javascript test is testing supposedly numerical values as strings, when
> they should be compared by actual value.
<snip>
> Is there a way I can cast values in javascript?
You can use the parseInt() function to convert them to integers:
if (parseInt(bid.previousbid.value) > parseInt(bid.nextbid.value) )
{
alert("Please enter a bid greater than $" + bid.previousbid.value + ".");
return false;
}
else
{
document.bid.submit();
return true;
}
You should also check to make sure they actually *are* integers. If
parseInt() can't convert them to integers for any reason it returns a
constant called NaN (= Not a Number), so you should also check for that
somewhere in your form validation function, e.g.:
if (parseInt(bid.previousbid.value) == NaN || parseInt(bid.nextbid.value)
== NaN)
{
alert("Naughty!");
return false;
}
(If you decide you want to allow non-integer amounts (dollars and cents) you
might want to use parseFloat() instead.)
HTH
Jason
More information about the thelist
mailing list