radio button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • change
    New Member
    • Dec 2007
    • 26

    radio button

    scribes

    I have the following code for radio buttons
    dim line
    <input type ="radio" name="line_type " value="isdn"> isdn
    <input type ="radio" name="line_type " value="lse"> lse

    what i want is that for a button when clicked
    if isdn is checked then line=isdn
    else
    if lse is checked then line=lse
    else
    alert ("no option selected")
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi change,

    There are actually two parts to your question here; assiging a value from a radio button & validating your form.

    If you want to assign a value to the variable sLine then you need to submit your form back to the same page and request the value of the radio button. Here is an example:

    thispage.asp:
    Code:
    <%
    Dim sLine
    sLine = request.form("line_type")
    %>
    <form name="form1" action="thispage.asp" method="post">
    	 <input type ="radio" name="line_type" value="isdn">isdn</input>
    	 <input type ="radio" name="line_type" value="lse">lse</input>
    	 <input type="submit" value="Submit" /> 
    </form>
    If you want to add some validation (in this case checking whether a radio button has been selected) then you could use some javascript. You could add something like this in between your head tags:
    Code:
     
    <script type="text/javascript">
    function RadioChecked()
    {
    var checked = false;
    for (var i=0;i<document.form1.line_type.length;i++)
    {
    	if (document.form1.line_type[i].checked)
    	{
    	 checked = true; 
    	}
    }
    if (checked == true)
    	{
    	return true;
    	}
    else
    	{
    	alert('No option selected') 
    	return false;
    	}	
    }
    </script>
    Then add OnClick="return RadioChecked(); " to your submit button.

    The above script loops through all the check boxes in the list and returns a value of 'true' to your form if it finds one that is checked, allowing it to submit.

    Does this all make sense? Let me know how it goes,

    Best regards,

    Dr B

    Comment

    Working...