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 Coder
    Join Date
    Feb 2009
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Problem in javacript code ?????

    Hi all,

    I have made to select box in which one is adult and other is for child .
    adult select box have 1 to 10 numeric value and child have 1 to 10 numeric value.

    but now i want to make if both of select box total is greater than 10 then it will give alert that Total adult and child can not be greater than 10 and it will return false.

    i have used this javascript code for this



    var tot=document.form1.adult.value + document.form1.child.value ;
    document.write(tot);
    if (tot >= "10")
    {
    alert ("Total value can not be greater than 10");
    document.form1.child.focus();
    return false;
    }


    Here like adult have value 4 and child have value 8 then tot should show
    12 but it is showing 48 .

    So how i can show this as a sum tot
    my means how i can show value 12 for this.
    Thanks
    Gagan

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    var tot=parseInt(document.form1.adult.value) + parseInt(document.form1.child.value) ;
    Code:
     if (tot >= 10)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    errm ... over 4 years since I last wrote Java scripts ... why im having trouble with my own :S

    But ... your line "var tot=document.form1.adult.value + document.form1.child.value ;" isn't that a concatination? From memory that it is taking the value for adult and the value for child and displaying them together as a single string.

    I'm thinking you would have to convert the values to Integers first, then do the calculations, then have the new total display.


  •  

    Posting Permissions

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