Click to See Complete Forum and Search --> : comparing negative numbers
pbodo
September 29th, 2002, 03:42 PM
The following JavaScript function is supposed to compare two numbers entered in a form and print "yes" if the first number is smaller than the second. It works for two positive numbers; it can compare a positive and a negative number; it cannot compare two negative numbers. Could anyone explain why? Thanks in advance.
function chek(form) {
if (form.elements["a" + 1 + 1].value < form.elements["b" + 1 + 1].value)
form.elements["c" + 1 + 1].value = "Yes";
}
Zvona
September 30th, 2002, 04:10 AM
<!-- Example Written by Zvona -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Untitled Document</title>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1;" />
<style type="text/css">
</style>
<script type="text/javascript">
<!--
function isGreaterThan(oForm)
{
var fValue1 = parseFloat(oForm.elements[0].value);
var fValue2 = parseFloat(oForm.elements[1].value);
var bCorrect = !(isNaN(fValue1) || isNaN(fValue2));
var oThird = oForm.elements[2];
oThird.value = (bCorrect)? (fValue1 > fValue2)? "Yes" : "No" : "Incorrect values";
return (bCorrect && (fValue1 > fValue2));
}
// -->
</script>
</head>
<body>
<form action="">
<table>
<tr>
<td><label for="idFirst">First :</label></td>
<td><input id="idFirst" type="text" onkeyup="isGreaterThan(this.form);" /></td>
</tr>
<tr>
<td><label for="idSecond">Second :</label></td>
<td><input id="idSecond" type="text" onkeyup="isGreaterThan(this.form);" /></td>
</tr>
<tr>
<td><label for="idThird">First > Second :</label></td>
<td><input id="idThird" type="text" readonly="readonly" /></td>
</tr>
</table>
</form>
</body>
</html>
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.