Calendar Custom Validator

Can someone suggest how to implement a custom validator for a Web Forms Calendar control? Apparently, neither RequiredValidator nor CustomValidator work out of the box with the Calendar control.

One solution offered by Microsoft is to extend the Calendar:

How to extend a Web form control to work with the validation controls by using Visual C#

Is there not a simpler solution?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I finally got it to work this way:

<asp:Calendar ID="startCalendar" CssClass="startDate" 
    OnSelectionChanged="Selection_Changed" runat="server"></asp:Calendar>
<asp:CustomValidator ID="dateCustVal" OnServerValidate="DateCustVal_Validate" 
    runat="server"></asp:CustomValidator>

protected void DateCustVal_Validate(object source, ServerValidateEventArgs args)
{            
    if (startCalendar.SelectedDate == null 
        || startCalendar.SelectedDate == new DateTime(0001, 1, 1, 0, 0, 0))
    {
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
}

Method 2

I think you should use Ajax Control Toolkit (downloadale from: http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/)
Then you can format your calendar as you want. An example is like this:

<ajaxToolkit:Calendar runat="server"
TargetControlID="Date1"
CssClass="ClassName"
Format="MMMM d, yyyy"
PopupButtonID="Image1" />

If you need to make sure that a date has been chosen, then you can do the following:

<ajaxToolkit:ValidatorCalloutExtender 
runat="Server"
ID="PNReqE"
TargetControlID="Date1" 
Width="350px"
HighlightCssClass="highlight" 
CssClass="CustomValidatorCalloutStyle"
PopupPosition="Right"
WarningIconImageUrl="warning.gif"
CloseImageUrl="close.gif" />


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x