views:

34

answers:

2

I have two sets of drop downs for start date and end date. Each date is created by selecting a month, day, and year from 3 separate drop downs. I currently have RequiredFieldValidators on all the drop downs (which just shows a * if nothing has been selected yet), but I need to validate that the end date is greater than the start date. I can take care of the logic behind comparing the dates, but in terms of the validation method used, can someone help me out (I essentially need to validate 6 drop downs all at one time)? I tried a custom validation using client side javascript but couldn't get it to work. Can you even validate multiple drop downs using ASP.NET validation controls? (which is what I would like to do - I can always write the javascript, but was trying to stay away from this).

Thanks.

+1  A: 

Hi Josh, you should consider using a calendar control instead of three dropdowns. It will give your users a better experience, and most calendar controls these days will automatically take care of date validation issues like leap years, localized names, etc. for you. On the server side, you would end up getting a valid DateTime value to work with, and you can use standard comparison operators to determine if one Date is before the other.

Telerik has a particularly good calendar control. Here's a demo:

http://demos.telerik.com/aspnet-ajax/calendar/examples/datepicker/validation/defaultcs.aspx

Warren
While I agree with the message here, it's worth nothing there are plenty of **free** options to do this that are excellent as well...it's client-side, doesn't necessarily have to be ASP.Net related at all.
Nick Craver
Historical dates like birthdays also tend to be a pain with certain calendar controls due to all the paging necessary to move back in time. Sometimes a few DDLs is a much better option. Though I appreciate the perspective.
Laramie
Hi Nick, there are indeed some good free client-only calendar libraries out there (e.g. jQuery UI Datepicker), but even with those libraries you still need to take server-side validation of user input into account. You have to tread carefully here if you need to support multiple date formats for different locales; good server-side integration with .NET Globalization is therefore a benefit... a control can say "okay, I'm going to be using ja-JP to display this calendar, therefore I better parse the date format in ja-JP when it comes back to me!"
Warren
@Nick Craver - how do I get the values of non-asp controls in my C# code? I am now using a jquery datepickers...
Josh
+1  A: 

Use a custom validator without the control to validate field completed then use this in your aspx page:

<script type="text/javascript">
<!--
    ValidatorHookupControlID("<%= ctrl1.ClientID %>",
     $get("<%= customValidator.ClientID %>"));
    ValidatorHookupControlID("<%= ctrl2.ClientID %>",
     $get("<%= customValidator.ClientID %>"));
    ValidatorHookupControlID("<%= ctrl3.ClientID %>",
     $get("<%= customValidator.ClientID %>"));
//-->
</script>

Edit: I found a tutorial that better explains what I mean here

Laramie
I never knew these existed. Thanks!
TheGeekYouNeed