SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2000
    0 Post(s)
    0 Thread(s)
    Hello All

    I have a question.

    I have a form, and I ask people questions, one of the questions are radio type. they have the option of choosing 3, but the have to choose one of them. What would the code look like to check if they have choosen one of them.

    The code I have for the other <input text> text types.
    <td width=200 align="right" id=tabletext bgcolor="#C6E7DE"><br>Requestor Name&nbsp;&nbsp;</td>
    <td id=tabletext bgcolor="#C6E7DE"><br><INPUT TYPE=text NAME="requestorName" WIDTH=30 onFocus="show('requestor')" onBlur="hide('requestor')">
    <font color="#08296B">*</font></td>

    and the javascript code that checks if there is an error...
    if (form.requestorName.value == "") {
    alert ("You must enter your name in the Requestor field.")
    return false
    now the code to look for the 3 radio buttons.
    <td width=200 align="right" id=tabletext bgcolor="#C6E7DE">Vendor named above will provide&nbsp;&nbsp;</td>

    <td id=tabletext bgcolor="#C6E7DE">
    <input type="radio" name="vendor" value="goods_services" onFocus="show('vendorD')" onBlur="hide('vendorD')">GOODS AND SERVICES
    <input type="radio" name="vendor" value="services" onFocus="show('vendorD')" onBlur="hide('vendorD')">SERVICES ONLY
    <input type="radio" name="vendor" value="goods" onFocus="show('vendorD')" onBlur="hide('vendorD')">GOODS ONLY
    <font color="#08296B">*</font>
    Now all I need is a javascript code to check if they entered in a value.

  2. #2
    I believe you have my stapler. scrubz's Avatar
    Join Date
    Feb 2001
    Van down by the river
    0 Post(s)
    0 Thread(s)
    This should do it:

    <script language="JavaScript">
    function checkRadios() {
    var frm = document.myForm, blanks = 0;
    for (i=0;i<frm.myRadio.length;i++)
    		{ if (frm.myRadio[i].checked == false)
    			{ blanks += 1 } 
    if (blanks == frm.myRadio.length)
    	{alert('Please select an option.')
    	 return false;}
    // -->
    Just replace myForm with the name of your form, and replace myRadio with the name of your radio button. This will work no matter how many radio buttons you have.


Posting Permissions

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