Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Program wont read string as a number

    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">
    <html>
    <head>
    <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;
    }
    }

    </script>
    </head>
    <body>
    <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)" />
    </form>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Aug 2005
    Location
    Toronto, ON, Canada
    Posts
    231
    Thanks
    0
    Thanked 0 Times in 0 Posts
    exactly
    if (Field1.value > Field2.value) {
    is a string comparison as they are text values

    if (parseInt(Field1.value) > parseInt(Field2.value)) {
    could help
    rm -f /

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, it works fine now!


  •  

    Posting Permissions

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