Empty Field (Radio button) validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SreeVidya Balasubramanian
    New Member
    • Mar 2007
    • 1

    Empty Field (Radio button) validation

    I am using a set of radio buttons in my asp.net (using C#) application. On click of the submit button i need to confirm whether the radio buttons have been checked using javascript. i am trying to take the value using the following line of code.

    if (document.getEl ementById("rbtn list_Q1").value == "")
    {
    alert("Please choose an option for Question no:1");
    }


    but the value remains undefined. How do i validate for unchecked radio buttons???
  • vee10
    New Member
    • Oct 2006
    • 141

    #2
    hi,

    The below code may solve ur problem

    if(document.get ElementById("rb tnlist_Q1").che cked==false)
    alert("Please choose an option for Question no:1");


    Originally posted by SreeVidya Balasubramanian
    I am using a set of radio buttons in my asp.net (using C#) application. On click of the submit button i need to confirm whether the radio buttons have been checked using javascript. i am trying to take the value using the following line of code.

    if (document.getEl ementById("rbtn list_Q1").value == "")
    {
    alert("Please choose an option for Question no:1");
    }


    but the value remains undefined. How do i validate for unchecked radio buttons???

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      To validate for radio buttons, you have to go through the array of radio buttons and check each one separately to see if it has been checked. See this link. That should solve your problem.

      Comment

      • gauravgmbhr
        New Member
        • Feb 2007
        • 107

        #4
        Originally posted by acoder
        To validate for radio buttons, you have to go through the array of radio buttons and check each one separately to see if it has been checked. See this link. That should solve your problem.


        TRY the following code


        m suppossing that ur form name is ="form"


        Code:
        function alert_empty_radio()
        {
        for(i=0;i<document.form.radios.length;i++)       
        
        if(document.form.radios[i].checked==false)
        {
        flag=1;
        }
        else {
        flag=0;
        break;
        }
        }  //end loop
        
        
        
        if(flag==1)
        {
        alert('ur custom message');
        }
        }//end function

        Comment

        Working...