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

    validate both the Radio button and the Select Option box

    Hello,

    I have been trying to validate both the Radio button and the Select Option box, but it does not work. It didn't show error, only din't do the way I want.

    I like to validate like this:

    If a user check "Yes" on this Radio button, then the OPTION box is disabled and no need to validate. However,
    when I tried the JS code below, the pop up window kept asking for a user to enter the "Seller" name.

    I combined both together as:
    Code:
    if (document.frmSelect.sellers.disabled=false && document.frmSelect.sellers.selectedIndex==""
    Can anyone please help?

    Thank you very much!

    html code
    Code:
       Sellers:
       <input type="radio" name="SellerName" onClick="document.getElementByID('sellers').disabled=true;" id="et1" value="Yes">Yes
       <input type="radio" name="SellerName" onClick="document.getElementByID('sellers').disabled=false;" id="et2" value="No">No
    <br>
      Names:
    <% 
    	  strSQL = "SELECT sellerLast, sellerFirst " _
           & " FROM tblComboSelect " _
    	   & " WHERE Cars= '" & trim(request.form("Cars")) & "' and DateSold BETWEEN DateAdd('m', -9, Date()) AND Date(); "
          Set oRs=Server.CreateObject("adodb.recordset")
          oRs.Open strSQL, myConn
          if not oRs.eof then %>
             <select name="sellers">
    		 <OPTION VALUE = "1" ></option>
    <%       do until oRs.eof %>
                <option value="<%= oRs("sellerLast") %>, <%= oRs("sellerFirst") %>"><%= oRs("sellerLast") %>, <%= oRs("sellerFirst") %></option>
    <%          oRs.MoveNext
             loop %>
             </select>
    <%    else %>
             <i>No records found for that car</i>
    <%    end if %>
    JS code:
    Code:
    function chForm() {
    	var errs = 0
    	var msgbox = "You forgot the following question (s).\nPlease complete and re-submit. Thanks!\n";
    	var goon = msgbox
    	var ctr = 0;
    	
    	var radCk = false;
    		for (i=0; i < document.frmSelect.SellerName.length; i++) {
    			if (document.frmSelect.SellerName[i].checked)
    				radCk = true; }
    			if (!radCk) {
    				msgbox = msgbox + "\n Please check one box";
    				}
    	if (document.frmSelect.sellers.disabled=false && document.frmSelect.sellers.selectedIndex=="") {
    		msgbox = msgbox + "\n Please select seller names";
    	}
    	
    	if (document.frmSelect.cars.selectedIndex=="") {
    		msgbox = msgbox + "\n Please select car type";
    	}
    	
    	if (document.frmSelect.q5.value=="") {
    		msgbox = msgbox + "\n Please add your comments";
    	}
    
    // If no errors found, then go ahead to submit the form
    	if (msgbox == goon) {
    		return true;
    	} else {
    
    alert(msgbox);
    return false;
    }
    }

  • #2
    New Coder
    Join Date
    Jan 2009
    Posts
    42
    Thanks
    2
    Thanked 6 Times in 6 Posts
    Here's an example that will enable/disable a select element based on which radio is checked. This works in FF but you may need to tweak it to get it to work in IE.
    Code:
    <html>
    <head>
    <script>
    function test(rad) {
       var sel = document.getElementById('sel');
       if (rad.value == 'Yes') {
          sel.setAttribute('disabled','true');
       } else {
          sel.removeAttribute('disabled');
       }
    }
    </script>
    </head>
    <body>
    <form>
    <input type="radio" name="rad" value="Yes" onclick="test(this);"/> One<br>
    <input type="radio" name="rad" value="No" onclick="test(this);"/> Two<br><br>
    <select name="select" id="sel">
    <option value="one">One</option>
    <option value="two">Two</option>
    </select>
    </form>
    </body>
    </html>

  • #3
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Code:
    function test(rad)
    	{
    	var sel = document.getElementById("sel");
    	if (rad.value == "Yes")
    		{
    		sel.disabled = true;
    		if (sel.setAttribute) sel.setAttribute("disabled","true");
    		}
    	else
    		{
    		sel.disabled = false;
    		if (sel.removeAttribute) sel.removeAttribute("disabled");
    		}
    	}
    AFAIK, the red works in all browsers though setAttribute is more XHTMLish.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #4
    Regular Coder fishluvr's Avatar
    Join Date
    Nov 2005
    Posts
    110
    Thanks
    1
    Thanked 12 Times in 12 Posts
    At a glance, I see a few issues...

    1) Used assignment (=) in condition, should be: if (document.frmSelect.sellers.disabled==false &&...

    2) Radio button onClicks should have lowercase "d" in getElementById().

    3) If using getElementById('sellers'), your select element should have an id attribute, not just a name.
    ><((((>`..`..`...><((((>`..`..`.. ><((((>`..`..`...><((((>

  • #5
    Regular Coder
    Join Date
    Nov 2005
    Posts
    225
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks so much.
    It worked perfectly.


  •  

    Posting Permissions

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