Results 1 to 7 of 7

Thread: Validating Radio / Option boxes

  1. #1

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Validating Radio / Option boxes

    I'm writing an asp page that's using vbs, js and a access DB.
    It's using VBS to build a table of 'clients' from the DB
    The first field in each row is a radio/options button who's value is assigned the clientID from the DB
    I need to make sure the user has selected one of the option boxes when they hit the Submit button.
    But it's not working, I keep getting 'undefined' for frmAdmin.optClientInfo.value
    I'm using similar code to validate text boxes and it works great.

    Can someone show me the problem here?

    Code:
    <HEAD>
    <title>Admin Page</title>>
    <SCRIPT language="JavaScript">
    function ValidateData()
    {
    	var sMsg = ""
    
    // The next line is debugging test, but it returns 'undefined'
    alert(frmAdmin.optClientInfo.value);
    
        if (frmAdmin.optClientInfo.value == "")
        {
    		sMsg = "Please select an account."
        }
    	else if (frmAdmin.optAction.value == "")
        {
    		sMsg = "Please select an Action."
    	}
    
        if (sMsg != "")
        {
    		alert(sMsg);
            event.returnValue = false
        }
    	    //else the form will be submitted automatically
    }
    </SCRIPT>
    
    </HEAD>
    
    ' I'm skipping a lot of code here
    ' it's using VBS to build a table of 'clients' from an access DB
    ' the first field in each row is a radio/options button who's value is assigned the clientID from the DB
    
    <FORM NAME="frmAdmin" METHOD="post" OnSubmit="ValidateData()" ACTION="AdminEdit.asp">
    
    'This next line is nested inside a loop for each 'client'
    <td><INPUT TYPE="Radio" NAME="optClientInfo" VALUE=<%=lClientId%>></td>

  2. #2
    Addicted Member
    Join Date
    Jan 2007
    Posts
    143

    Re: Validating Radio / Option boxes

    try checking the ".checked" property. loop through the radio buttons (using the name) and if none of the .checked properties hold "true", then not a single one was selected. hope this helps.

  3. #3
    Addicted Member
    Join Date
    Aug 2007
    Location
    India
    Posts
    141

    Re: Validating Radio / Option boxes

    Just change two lines like:

    if (!frmAdmin.optClientInfo.checked) {

    else if (!frmAdmin.optAction.checked) {


    and your code will work fine

  4. #4

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Validating Radio / Option boxes

    No luck.
    Using Checked creates the alert even when one of the option buttons is selected.

    And the following code still returns 'undefined'.
    I get the impresion that the JS just isn't seeing the optClientInfo's
    Code:
    alert(frmAdmin.optClientInfo.checked);
    Maybe it's because they are an array?
    How would you loop through them when you don't know how many will be on the page?
    Last edited by longwolf; Aug 31st, 2007 at 06:39 AM.

  5. #5

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Validating Radio / Option boxes

    I just tried this and it works,
    Code:
    alert(frmAdmin.optClientInfo(1).checked);
    so I just need to know how to loop through them using js.
    Can someone show me?

  6. #6
    Addicted Member
    Join Date
    Aug 2007
    Location
    India
    Posts
    141

    Re: Validating Radio / Option boxes

    i have just changed your code and its working fine.

    vb Code:
    1. <HEAD>
    2. <title>Admin Page</title>>
    3. <SCRIPT language="JavaScript">
    4. function ValidateData()
    5. {
    6.     var sMsg = ""
    7.  
    8. // The next line is debugging test, but it returns 'undefined'
    9.  
    10. alert(frmAdmin.optClientInfo.value);
    11. alert(frmAdmin.optClientInfo.checked);
    12.     if (!frmAdmin.optClientInfo.checked)
    13.     {
    14.         sMsg = "Please select an account."
    15.     }
    16.     else if (frmAdmin.optAction.checked == "")
    17.     {
    18.         sMsg = "Please select an Action."
    19.     }
    20.  
    21. alert(sMsg)
    22.     if (sMsg!= "")
    23.     {
    24.         alert(sMsg);
    25.         event.returnValue = false
    26.     }
    27.         //else the form will be submitted automatically
    28. }
    29. </SCRIPT>
    30.  
    31. </HEAD>
    32.  
    33. ' I'm skipping a lot of code here
    34. ' it's using VBS to build a table of 'clients' from an access DB
    35. ' the first field in each row is a radio/options button who's value is assigned the clientID from the DB
    36.  
    37. <FORM NAME="frmAdmin" METHOD="post" OnSubmit="ValidateData()" ACTION="AdminEdit.asp">
    38.  
    39. 'This next line is nested inside a loop for each 'client'
    40. <INPUT TYPE="Radio" NAME="optClientInfo" VALUE=<%=lClientId%>>
    41. <INPUT type="button" onclick="ValidateData()" value="test">
    42. </BODY>
    43. </HTML>

    will you pass on the complete code?

  7. #7

    Thread Starter
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Validating Radio / Option boxes

    Thx vksingh24,
    But I tried those changes to the ValidateData function and they didn't work.

    I think what you missed is that a separate optClientInfo is made for each Client in the DB.

    So we're dealing with a control array, and not a single radio/ options button(see my last post)

Posting Permissions

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



Click Here to Expand Forum to Full Width