CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2002
    Posts
    2

    comparing negative numbers

    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";

    }

  2. #2
    Join Date
    Jan 2002
    Location
    Helsinki, Finland
    Posts
    99
    Code:
    <!-- 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 &gt; Second :</label></td>
    				<td><input id="idThird" type="text" readonly="readonly" /></td>
    			</tr>
    		</table>
    		</form>
    	</body>
    </html>
    Zvona - First aid for client-side web design

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured