SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2002
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wrapping Javascript to avoid execution

    I have two radio buttons on a form with some input boxes. The input boxes are arrays built on the number necessary gathered from the database. One set is for percentages, and the other set is for cash totals. I need to be able to wrap the javascript with an if statement that only allows the script to run when the radio button for the percentage fields is checked. The radio buttons are called radiocommtype. They have values of percentage and cashtotal. I only want to process the javascript below if the percentage radio button is checked. If it is not then it will bypass the javascript, seems simple enough. But it keeps wanting to jump into the javascript even when the button is not checked. Here is my code:

    <script type='text/javascript' language='javascript'>
    function filter(e, field) {
    if (document.test.radiocommtype[0].checked == true){
    var kC = (e && e.keyCode) ? e.keyCode :
    (e && e.which) ? e.which : null;
    if (!kC || kC == 8) return true;
    else return (kC>47 && kC<58);
    }
    }

    function sumPctOnblur(field) {
    if (document.test.radiocommtype[0].checked == true){
    var el, e = 0, total_pct = 0, f = field.form;
    if (typeof f[field.name].length == 'undefined') {
    field.value = '100';
    f.TOTALPERCENT.value = '100';
    alert('Since you are a single agent on this deal, your split will be automatically converted to 100%.');
    return;
    } else {
    if (!field.value) return;
    field.value = parseInt(field.value);
    while (el = f[field.name][e++])
    if (/^\d{1,3}/.test(el.value)) total_pct += parseInt(el.value);
    f.TOTALPERCENT.value = total_pct;
    }
    }
    }

    function sumPctOnsubmit(grp) {
    if (document.test.radiocommtype[0].checked == true){
    var el, e = 0, total_pct = 0;
    if (typeof grp.length == 'undefined') {
    grp.value = '100';
    grp.form.TOTALPERCENT.value = '100';
    return;
    } else {
    while (el = grp[e++])
    if (/^\d{1,3}/.test(el.value)) total_pct += parseInt(el.value);
    else {alert('Please enter a valid percentage (n%) for all agents.');
    el.focus();
    el.select();
    return false;
    }
    if (total_pct != 100) {
    var msg = 'The total of all agents percentages should be 100%, please adjust your percentages.';
    alert(msg);
    grp[0].focus();
    grp[0].select()
    ;//optional
    return false;
    }
    }
    return true;
    }
    }

    </script>

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please post the HTML that uses these functions. It doesn't have to be the whole page - just the form or part of the form will suffice.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Member
    Join Date
    Apr 2002
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <form action="upd-commission.php" name = 'test' method="get" action="javascript:alert('Submitted !')" onSubmit="return sumPctOnsubmit(this['PERCENTAGE[]'])">


    <tr>
    <td width = '2%' colspan = '4'>&nbsp;</td>
    <td width = '2%' ><input name="commissiontype" type="radio" value="percentage" ></td>
    <td width = '1%'>&nbsp;</td>
    <td width = '1%'>&nbsp;</td>
    <td width = '1%'>&nbsp;</td>
    <td width = '92%' ><input name="commissiontype" type="radio" value="cashtotal" ></td>
    </tr>

    <td width="5%" ><input type="text" name="PERCENTAGE[]" value="<? echo $percentage;?>" size="5" maxlength="3" onBlur="sumPctOnblur(this)" onKeyPress="return filter(event,this)"></td>
    <td width="3%" nowrap ><span class="gry">%</span>&nbsp;</td>
    <td width="3%" nowrap >OR</td>
    <input type="hidden" name="agent_num[]" value="<? echo $agent_num;?>">
    <!-- <td width="10%" nowrap align = "center"><span class="gry">OR</span>&nbsp;</td>-->
    <td width="2%" nowrap align = "right"><span class="gry">$</span>&nbsp;</td>
    <td width="10%">&nbsp;<input type="text" name="TOTAL[]" value="<? echo $cashshare;?>" size="10" maxlength="20" style="background-color : Yellow;"></td>


Bookmarks

Posting Permissions

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