2

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?

ozkank
  • 1,464
  • 7
  • 32
  • 52
  • Write some jQuery. Catch the 'change' event from the radiobuttonlist, and change the validation.. Perhaps make 2 different inputfields, and hide/show them in the eventhandler. – Rob Mar 16 '12 at 08:03

2 Answers2

0

There are a number of ways to do this:

  1. 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)

  2. Let them enter whatever they want and do a server side validation on post back.

  3. Allow them to enter whatever they want and do a client side validation using jquery on button click.

  4. 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.

Community
  • 1
  • 1
Divi
  • 7,621
  • 13
  • 47
  • 63
-1

Add a regular expression validator control to the page. Make it invisble. When you select the numeric option, then in the code behind make this regular expression validator control visible, and when you select the other option make sure to hide it again. Now when you click the button (when the numeric option is selected) then it will fire the regular expression (to check for numeric values only.

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
  • 1
    Validation will not come into play until you change the validation group of the click event. – Pankaj Mar 16 '12 at 08:43