Josh
Josh

Reputation: 3611

ASP.NET: What's the best way to validate 3 drop downs boxes that are used to select a date (month, day, year)?

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.

Upvotes: 3

Views: 1942

Answers (2)

Laramie
Laramie

Reputation: 5587

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

Upvotes: 5

Warren Rumak
Warren Rumak

Reputation: 3864

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

Upvotes: 1

Related Questions