0

I am working with C#, asp.net 3.5. I have a dropdown list which has values like:

Pick last 24 hrs data
Pick last 48 hrs data
Pick data between dates

When I select Pick data between dates it shows two textboxes where user can set "From Date" and "To Date"

I want to apply a validation that if user selects between dates option, dates' textboxes must be filled, for other dropdown values textboxes' values don't need to be checked.

Should I do it through jquery/javascript (which I am already doing) Or is there a way to achieve this by using existing validation controls ?

shrekDeep
  • 2,260
  • 7
  • 27
  • 39

3 Answers3

0

Yup , You can do it by compare validation

See this sample :

<asp:CompareValidator ID="cmpVal1" ControlToCompare="txtStartDate" 
         ControlToValidate="txtEndDate" Type="Date" Operator="GreaterThanEqual"   
         ErrorMessage="*Invalid Data" runat="server"></asp:CompareValidator>
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • But I want it to validate only when **Pick between dates** option is selected. Compare validator control is comparing values of two textboxes, but it doesn't check what value is there in dropdown, right ? – shrekDeep Dec 12 '13 at 07:52
  • Set visible property for your dropdown changing values by using Javascript – Ramesh Rajendran Dec 12 '13 at 07:54
0

You may add custom validation methods in js.

Also you may add custom attribute on elements in your model (in C# code) and write your custom behavior (for example read this answer).

Community
  • 1
  • 1
Boris Parfenenkov
  • 3,219
  • 1
  • 25
  • 30
0

In my opinion if you even do this validation using javascript you should anyway duplicate same validation on server. In other case it is possible to disable javascript in browser and send not valid form to server. You should create javascript validation if you want to provide nice looking UX.

If you are looking for pure asp.net approach you can do as follows: 1) Set dropdown Autopostback="true" which will trigger postback each time you change value in drop down.

<asp:DropDownList id="ddl" 
       AutoPostBack="True"  
       runat="server">

2) Add validation controls for your date time fields.

<asp:RequiredFieldValidator />

3) Disable or enable validation controls during postback depending on selected drop down value.

Yevgeniy.Chernobrivets
  • 3,194
  • 2
  • 12
  • 14