View Full Version : Program wont read string as a number

09-17-2005, 08:02 PM
Any help would be great,

What the program should do: when a user enters numbers into the text boxes, the ascend button should put the numbers in ascending order (smallest number to largest number).

Now at the moment, if I enter lets say5 in textbox1 and 3 in textbox2, the program will order them in ascending order properly.
But lets say if I enter 9 in textbox1 and 49594 in textbox2, the program displays 9 in textbox2 as being a larger number

im guessing the computer is reading the number as a string and Im not sure how to use parseInt to make it read as a number.

Any help would be greatly appreciated. THank You!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<body bgcolor="yellow">
<title>STAGE 2</title>
<script type="text/javascript">
function ascend(Field1, Field2) {
if (Field1.value > Field2.value) {
var temp = Field1.value;
Field1.value = Field2.value;
Field2.value = temp;

<h1>STAGE 2</h1>
<p> <b>Note:</b> Only numerical values may be entered <p>
<form name="capsform">
<b>Text Box 1:</b><input type="text" name="text_box1" size="20" value="" onchange="this.value=this.value;" />
<br /><br />
<b>Text Box 2:</b><input type="text" name="text_box2" size="20" value="" onchange="this.value=this.value;" />
<br /><br />
<input type="button" value="Ascend" onclick="ascend(this.form.text_box1, this.form.text_box2)" />

09-17-2005, 10:25 PM
if (Field1.value > Field2.value) {
is a string comparison as they are text values

if (parseInt(Field1.value) > parseInt(Field2.value)) {
could help

09-17-2005, 11:35 PM
Thanks, it works fine now!