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 14 of 14
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Javascript Help Needed with Alert Message..

    Hi,

    I have the following code.
    <script>
    function prob()
    {
    if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value ==0 ))
    {
    alert("% GO Must be between 1 and 100.");nbdb_update.percentagego.value = "";
    }
    if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value ==0 ))
    {
    alert("%WIN must be between 1 and 100.");nbdb_update.percentagewin.value = "";
    }
    if ((isNaN(parseInt(document.getElementById("percentagego").value)) || isNaN(parseInt(document.getElementById("percentagewin").value))))
    {
    document.getElementById("probability").value="";
    }
    else
    {
    var val1 = parseInt(document.getElementById("percentagego").value);
    var val2 = parseInt(document.getElementById("percentagewin").value);
    var ansd = document.getElementById("probability");
    ansd.value = Math.round((val1 * val2)/100);
    }
    }
    </script>

    The problem here is the bothe the alert messages get displayed if the condition fails( i have marked the alert messages in bold)

    How can i change the code to display only message relaveant to that text box?

    Please help me.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Presumably you are saying that if both conditions fail both alerts are shown. You only want one.

    function prob() {
    var shown = 0;
    if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value <1 ))
    {
    alert("% GO Must be between 1 and 100.");nbdb_update.percentagego.value = "";
    shown = 1;
    }

    if (shown == 0) {
    if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value <1 ))
    {
    alert("%WIN must be between 1 and 100.");nbdb_update.percentagewin.value = "";
    }
    }

  • Users who have thanked Philip M for this post:

    coolguyraj (08-21-2007)

  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Thank You.

    This worked,

    Thanks a lot.

  • #4
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Sorry to bother you again,

    After i fill the first box with valid input and place the cursor on the net box i get the alert message for that box without any value in it.

    How can i fix that?
    function prob() {
    var shown = 0;
    if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value <1 ))
    {
    alert("% GO Must be between 1 and 100.");nbdb_update.percentagego.value = "";
    shown = 1;
    }

    if (shown == 0) {
    if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value <1 ))
    {
    alert("%WIN must be between 1 and 100.");nbdb_update.percentagewin.value = "";
    }
    }

    Example: if i have text1=20 and text2='blank'
    i get the alert "text 1 should be between 1 and 100"

    i don want this ti happen.

    please help.

    Thanks,

  • #5
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Small Problem

    Sorry to bother you again,

    After i fill the first box with valid input and place the cursor on the net box i get the alert message for that box without any value in it.

    How can i fix that?
    function prob() {
    var shown = 0;
    if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value <1 ))
    {
    alert("% GO Must be between 1 and 100.");nbdb_update.percentagego.value = "";
    shown = 1;
    }

    if (shown == 0) {
    if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value <1 ))
    {
    alert("%WIN must be between 1 and 100.");nbdb_update.percentagewin.value = "";
    }
    }

    Example: if i have text1=20 and text2='blank'
    i get the alert "text 1 should be between 1 and 100"

    i don want this ti happen.

    please help.

    Thanks,

  • #6
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post
    hi
    Can you post the whole code along with the HTML part.

    Thanks
    Amit

  • #7
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts
    <script>
    function prob() {
    var shown = 0;
    if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value <1 ))
    {
    alert("% GO Must be between 1 and 100.");nbdb_update.percentagego.value = "";
    shown = 1;
    }

    if (shown == 0) {
    if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value <1 ))
    {
    alert("%WIN must be between 1 and 100.");nbdb_update.percentagewin.value = "";
    }
    }
    if ((isNaN(parseInt(document.getElementById("percentagego").value)) || isNaN(parseInt(document.getElementById("percentagewin").value))))
    {
    document.getElementById("probability").value="";
    }
    else
    {
    var val1 = parseInt(document.getElementById("percentagego").value);
    var val2 = parseInt(document.getElementById("percentagewin").value);
    var ansd = document.getElementById("probability");
    ansd.value = Math.round((val1 * val2)/100);
    }
    }
    </script>

    HTML part <tr>
    <th class="bodyText" align="right">% Go:&nbsp;&nbsp;</th>
    <td class="bodyText" colspan="2"><input type="text" id="percentagego" name="percentagego" onblur="prob()" maxlength="3" value="<?php
    If($percentagego_update=="00"){
    echo "100";
    }else
    echo $percentagego_update?>" size="17"></td>
    </tr>
    <tr>
    <th class="bodyText" align="right">% Win:&nbsp;&nbsp;</th>
    <td class="bodyText" colspan="2"><input type="text" id="percentagewin" name="percentagewin" onblur="prob()" maxlength="3" value="<?php
    If($percentagewin_update=="00"){
    echo "100";
    }else
    echo $percentagewin_update;?>"size="17"></td>
    </tr>


    <tr>
    <th class="bodyText" align="right">Probability:&nbsp;&nbsp;</th>
    <td class="bodyText" colspan="2"><input type="text" readonly="readonly" onblur="prob()" name="probability" id="probability" value="<?php
    if (!empty($probability_update)) {
    echo number_format($probability_update,0);
    }?>" maxlength="3" size="5"></td>
    </tr>

  • #8
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hi Raj,
    The problem is because you are calling the function using the onBlur functionality of Javascript.
    You need to change that to onChange.
    Please find below, a sample code that is designed using the basic 2text boxes, and it is working fine.

    You can directly paste it in the HTML and run.

    </html>
    <script>
    function prob() {
    alert("getting called");
    var shown = 0;
    if ((document.getElementById("percentagego").value >100 )||(document.getElementById("percentagego").value <1 ))
    {
    alert("% GO Must be between 1 and 100.");document.forms[0].percentagego.value = "";
    shown = 1;
    }

    if (shown == 0) {
    if ((document.getElementById("percentagewin").value >100 )||(document.getElementById("percentagewin").value <1 ))
    {
    alert("%WIN must be between 1 and 100.");
    document.forms[0].percentagewin.value = "";
    }
    }
    }
    </script>


    <form>
    <input type="percentagego" name="percentagego" onChange="prob()">
    <input type="percentagewin" name="percentagewin" onChange="prob()">

    </form>


    I Hope your problem will be solved on doing these changes.

    Regards
    Amit.

  • #9
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi Amit,

    Thanks for the solution.

    I still have the problem the code works fine but

    Here is the problem.

    1. i have two text boxes blank.
    2 i fill the first text box with valid input and move to the second text box(now i get the alert message saying the "text2 is invalid) I Dont want this to appear unless the data is feed to the box.

    Thanks,
    RAJ

  • #10
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hi Raj
    can you confirm that you are using onChange in all the Input text fields.
    if you are doing that, then problem should not occur.

    Else please post the code once again, and how to run that.

    Regards
    Amit

  • #11
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hi Amit,

    Here is the code.

    My main aim is not to display anything related to the second text box untill i fill something it (May be an invalid value,in our case number >100)


    <script>
    function prob()
    {
    document.getElementById("probability").value = "";
    var percgo = parseInt(document.getElementById("percentagego").value);
    if(isNaN(percgo) || percgo < 1 || percgo > 100) {
    alert("% GO Must be a Number and Between 1 and 100.");
    nbdb_update.percentagego.value = "";
    return;
    }
    document.getElementById("probability").value = "";
    var percwin = parseInt(document.getElementById("percentagewin").value);
    if(isNaN(percwin) || percwin < 1 || percwin > 100) {
    alert("% WIN Must be a Number and Between 1 and 100.");
    nbdb_update.percentagewin.value = "";
    return;
    }

    var ansd = document.getElementById("probability");
    ansd.value = Math.round((percgo * percwin)/100);
    }
    </script>


    HTML CODE
    <tr>
    <th class="bodyText" align="right">% Go:&nbsp;&nbsp;</th>
    <td class="bodyText" colspan="2"><input type="text" id="percentagego" name="percentagego" onchange="prob()" maxlength="3" value="<?php
    If($percentagego_update=="00"){
    echo "100";
    }else
    echo $percentagego_update?>" size="17"></td>
    </tr>
    <tr>
    <th class="bodyText" align="right">% Win:&nbsp;&nbsp;</th>
    <td class="bodyText" colspan="2"><input type="text" id="percentagewin" name="percentagewin" onchange="prob()" maxlength="3" value="<?php
    If($percentagewin_update=="00"){
    echo "100";
    }else
    echo $percentagewin_update;?>"size="17"></td>
    </tr>


    <tr>
    <th class="bodyText" align="right">Probability:&nbsp;&nbsp;</th>
    <td class="bodyText" colspan="2"><input type="text" readonly="readonly" onblur="prob()" name="probability" id="probability" value="<?php
    if (!empty($probability_update)) {
    echo number_format($probability_update,0);
    }?>" maxlength="3" size="5"></td>
    </tr>


    Basically i am trying to take input from two text boxes and calculating the result and displaying it in the third column.

    Thanks,
    RAJ

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    <td class="bodyText" colspan="2"><input type="text" readonly="readonly" onblur="prob()" name="probability"

    amitthechosen1 has pointed out what is wrong.

  • Users who have thanked Philip M for this post:

    coolguyraj (08-22-2007)

  • #13
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hi Raj,
    Hope the problem is solved.
    I dont have a set up for PHP else would have simulated the whole code and solved.
    If u can provide with what i need to do to compile, the I may be able to help.

    Regards,
    Amit

  • Users who have thanked amitthechosen1 for this post:

    coolguyraj (08-22-2007)

  • #14
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks Philip and Amit for all the help.

    I have fixed it.This is what i have done.function prob()
    {
    document.getElementById("probability").value = "";
    var percgo = parseInt(document.getElementById("percentagego").value);
    if(isNaN(percgo) || percgo < 1 || percgo > 100) {
    alert("% GO Must be a Number and Between 1 and 100.");
    nbdb_update.percentagego.value = "";
    return;
    }
    if(document.getElementById("percentagewin").value == "") {
    return;
    }
    var percwin = parseInt(document.getElementById("percentagewin").value);
    if(isNaN(percwin) || percwin < 1 || percwin > 100) {
    alert("% WIN Must be a Number and Between 1 and 100.");
    nbdb_update.percentagewin.value = "";
    return;
    }

    var ansd = document.getElementById("probability");
    ansd.value = Math.round((percgo * percwin)/100);
    }


  •  

    Posting Permissions

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