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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Two groups of Radio Buttons validation

    I have two groups of radio buttons and would like to allow only limited selection from the second group depending on what was selected in the first group. In my code below if a 'Classic' button is selected, I only want to allow the first, second or third year button to be selected. My radio_validate function tells me that one has been selected from each. I would like the plan_validate to happen if my hidden field called 'Classic' is equal to 'Y'.
    I listed the other functions because they are on my form submit.

    Please help.


    <script type="text/javascript" language="javascript">
    function validate(f)
    {
    var el, e = 0;
    {
    while (el = f.elements[e++]) {
    if (el.name == 'vlname' && !el.value) {
    alert('Please fill in your Last Name.');
    el.focus();
    return false;}
    else if (el.name == 'vfname' && !el.value) {
    alert('Please fill in your First Name.');
    el.focus();
    return false;}
    else if (el.name == 'vdr1' && !el.value) {
    alert('Please fill in your Street Address.');
    el.focus();
    return false;}
    else if (el.name == 'vcty' && !el.value) {
    alert('Please fill in your City.');
    el.focus();
    return false;}
    else if (el.name == 'vst' && !el.value) {
    alert('Please fill in your State (ex.TX).');
    el.focus();
    return false;}
    else if (el.name == 'vst' && !el.value) {
    alert('Please fill in your State (ex.TX).');
    el.focus();
    return false;}
    }
    }
    return true;}
    </script>

    <script language="JavaScript">
    function radio_validate(f) {
    if (!f) return;
    var currEl;
    for (var e=0; e<f.elements.length; ++e) {
    if (f.elements[e].type != 'radio' || !f.elements[e].name) continue;
    currEl = f.elements[f.elements[e].name];
    if (!currEl.length) continue;
    for (var idx=0; idx<currEl.length; ++idx) {
    if (currEl[idx].checked) break;
    }
    if (idx == currEl.length) {
    alert('Please select a Price Plan and the Number of Years.')
    currEl[0].focus();
    return false;
    }
    }
    return true;
    }
    </script>

    <script language="JavaScript">
    function plan_validate(f) {
    if (!f) return;
    var plan = document.getElementsByName('vprdtype');
    var year = document.getElementsByName('v1yr');
    if (plan[0].checked && year[1].checked) {
    alert('Classic Plan can only have year 1 selected');
    year.focus();
    return false;
    }
    }
    return true;
    }
    </script>

    <script language="JavaScript">
    function disableEnterKey(e)
    {
    var key;
    if(window.event)
    key = window.event.keyCode;
    else
    key = e.which;
    if(key == 13)
    return false;
    else
    return true;
    }
    </script>

    <form name="custdet" action="/cgi-bin/ajxx.pgm" onsubmit="return validate(this)&& radio_validate(this)&& plan_validate(this);" method="POST" onkeypress="return disableEnterKey(event);">
    <input type="hidden" name="Classic" size="1" value="Y">


    <tr>
    <td>
    <input type="radio" value="CLASSIC" name="vprdtype" ><b>CLASSIC</b>
    <input type="radio" value="GOLD" name="vprdtype" ><b>GOLD</b>
    <input type="radio" value="PLATINUM" name="vprdtype" ><b>PLATINUM</b>
    <input type="radio" value="PLATPLUS" name="vprdtype" ><b>PLATINUM PLUS</b>
    </span></td>
    </tr>
    <tr><td><b>Number of Years</b><span>
    <tr><td>
    <input type="radio" value="1" name="v1yr"><b>1&nbspYr.
    <input type="radio" value="2" name="v1yr"><b>2&nbspYr.
    <input type="radio" value="3" name="v1yr"><b>3&nbspYr.
    <input type="radio" value="4" name="v1yr"><b>4&nbspYr.
    <input type="radio" value="5" name="v1yr"><b>5&nbspYr.
    <input type="radio" value="6" name="v1yr"><b>6&nbspYr.
    <input type="radio" value="7" name="v1yr"><b>7&nbspYr.
    </td>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,047
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Add this at the start of the plan_validate function.
    Code:
    if (f.Classic.value != "Y") return true;


  •  

    Posting Permissions

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