I’ve a radiobutton list like this;
<asp:RadioButtonList ID="RadioButtonFirm" runat="server" RepeatDirection="Horizontal">
<asp:ListItem id="option1" runat="server" Value="Firm Code" />
<asp:ListItem id="option2" runat="server" Value="Firm Name" />
</asp:RadioButtonList>
and also an asp:button control
<asp:Button ID="btnSearchFirm" runat="server" OnClientClick="Validate();" Text="search" OnClick="btnSearchFirm_Click" />
and a textbox
<asp:TextBox ID="txtCriteria" Width="120px" runat="server"></asp:TextBox>
If I select Firm Code from radio button, user only select numeric value to txtCriteria textbox, else string can be entered. How to validate it?
There are a number of ways to do this:
Only allow the user to enter numbers in the textbox. Here’s a link with the example. Of course, you’ll have to add a condition when they can enter characters as well. jQuery: what is the best way to restrict "number"-only input for textboxes? (allow decimal points)
Let them enter whatever they want and do a server side validation on post back.
Allow them to enter whatever they want and do a client side validation using jquery on button click.
You can hide and show a numeric and a string textbox using jquery on radio button selection and add validtors on the numeric text-box.
Whatever you choose to do, always make sure that you always have server side validation to make sure absolutely that whatever data goes in your database is valid. The other reason, the users can have their javascript disabled (not very common though) or its a basic version of the website for screen-readers and so your client side validation will be disabled.