Link to home
Start Free TrialLog in
Avatar of Anthony Matovu
Anthony MatovuFlag for Uganda

asked on

using field validator on a radio button and text box

I have two options A and B
If the user selects A he is not supposed to enter anything for the text box.
if the user selects B then it is compulsory to enter a value in the text box.

I want to control this by a field validator in asp.net 3.5
Avatar of revaluser
revaluser
Flag of India image

Have a Custom Validator and Custom Script or use the javascript

and you did not specify that does the radio button selection is mandatory or not



<asp:LinkButton ID="lbnGeneral" runat="server" CssClass="leftmenu_link" OnClick="lbnGeneral_Click" OnClientClick="return ValidateCategory();" ValidationGroup="AddTopic" >General</asp:LinkButton>




function ValidateCategory()
{
      //Place your RadioButton List Id and textBox Id i just used for reference
      var rbnCategories= document.getElementById ("<%= rblCategory.ClientID %>");
     var rbnCategoryList= rbnCategories.getElementsByTagName("input");
     var checkCount = false;
     var j=0;
     for(var i=0;i<rbnCategoryList.length;i++)
        {  
            j=j+1;
            if(rbnCategoryList[i].checked)
            {
                checkCount = true;
      break;

            }
        }
       
        if(checkCount)
        {
              if(j==2)
               {
                  if(document.getElementById ("<%= TextboxId.ClientID %>").value =="")
                  {
                      alert("Please enter Some data into the Textbox");
                      return false;
                   }
                   else
                    {
                        return true;
                      }
               }
              else
              {
                   return true;
              }
        }
        else
        {

      alert("please select atleast one option");
             return false;    
        }
}
Avatar of Anthony Matovu

ASKER

Thanks you, I am using vb.net (sorry i was not very clear)

cant i use the compare validator?

the radio button selection is mandatory

Anthony
Avatar of shubham_gniiit
shubham_gniiit


You can solve your problem like this way.

<asp:RadioButtonList runat="server" ID="radio">
                <%--Creaint Items to be display in Radio Button List--%>
                <asp:ListItem Value="0">Male</asp:ListItem>
                <asp:ListItem Value="1">Female</asp:ListItem>
            </asp:RadioButtonList>
            <%--Creating RequiredFieldValidator control to validate RadioButtonList that we have created--%>
            <asp:RequiredFieldValidator runat="server" ID="radRfv" ControlToValidate="radio"  errormessage="Select One option"></asp:RequiredFieldValidator>
Eighther you can use initialvalue property required filed validator. which is like  

 <asp:RequiredFieldValidator runat="server" ID="radRfv" ControlToValidate="radio"  initialvalue="0" errormessage="Select one option"></asp
I want something like
if user has select option b and the text box is missing the raise the error massege
and
if user has select option a and the text box is not missing the raise the error massege

thank you
the solution which i had given earlier is nothing to do with vb.net or c# the function is a javascript function and when the user try to click the button  call that function the way which i did using the link button
 using OnClientClick="return ValidateCategory();"

it is a working solution just try if not i will provide the complete code
ASKER CERTIFIED SOLUTION
Avatar of revaluser
revaluser
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial