SharePoint DateTimeControl Validation

I have been using a DateTimeControl for a custom webpart and it is really nice (especially because I get a similar look and feel to the DateTimecontrols on the SharePoint list). However, I noticed one small drawback with it – Validation.
Internally the DateTimeControl of SharePoint is just a calendar dropdown and a text box. Also, they already implemented a simple required field validation by just using the IsRequiredField property. However, if you try to put an asp.net validator (i.e.: CompareValidator, RegularExpressionValidtator, etc…) on it, it says it can’t find the control. This is happening because the DateTimeControl is just a wrapper around this textbox and if you set the Validator ControlToValidate property to the DateTimeControl it isn’t smart enough to know to really validate against the actual textbox.
So, how do we accomplish this. Actually, it is pretty easy – SharePoint is giving an ID to that Textbox programatically and this ID is the same name as your control with the word “Date” added to the end.
So, in your ControlToValidate just put “{nameofcontrol}${nameofcontrol}Date”.
Here is an example:


<SharePoint:DateTimeControl ID="dteDemo" runat="server" DateOnly="true" />
 <asp:CompareValidator id="valDate" runat="server"
         ForeColor="Red"
         ControlToValidate="dteDemo$dteDemoDate"
         Type="Date"
          Operator="DataTypeCheck"
          ErrorMessage="Please enter an valid date">
</asp:CompareValidator >

17 Responses to “SharePoint DateTimeControl Validation”

  1. Dew Drop - June 29, 2008 | Alvin Ashcraft's Morning Dew Says:

    [...] SharePoint DateTimeControl Validation (Greg Galipeau) [...]

  2. Links (6/29/2008) « Steve Pietrek - Everything SharePoint Says:

    [...] SharePoint DateTimeControl Validation [...]

  3. Bookmarks about Validation Says:

    [...] – bookmarked by 4 members originally found by ItachiUchiha27 on July 16, 2008 SharePoint DateTimeControl Validation http://greggalipeau.wordpress.com/2008/06/27/sharepoint-datetimecontrol-validation/ – bookmarked [...]

  4. Pushkar Says:

    brilliant…was stuck…your post helped me greatly…thanks :)

  5. Pallavi Says:

    The post is really helpful!

  6. Renat Says:

    Thanks a lot!
    Really clear description of the problem
    And nice solution

  7. gaurav Says:

    If you use compare validator and enter invalid date in textbox. the invalid date message will come but the error message will not get clear if you select the correct date from the calendar image icon.

  8. Использование Sharepoint DateTimeControl с валидаторами ASP.NET « Mikhail Buslaev’s blog Says:

    [...] SharePoint DateTimeControl Validation Possibly related posts: (automatically generated)Tolong di translateRussian Eee PC 901 looks good [...]

  9. Использование Sharepoint DateTimeControl с валидаторами ASP.NET « using Microsoft.SharePoint; Says:

    [...] SharePoint DateTimeControl Validation Possibly related posts: (automatically generated)Tolong di translateRussian Eee PC 901 looks good [...]

  10. Brad Says:

    This seems to only validate the date, Is there a way to specify Date and Time.

    Thanks!
    Brad

  11. Bob Says:

    I’ve tried your suggestion, but have trouble with the control name – on my DateTime fields, the names wind up something like:

    <input … name=”ctl00$m$g_0482d57d_fb6e_4f7d_883d_3e554001deb5$ctl00$ctl04$ctl02$ctl00$ctl00$ctl04$ctl00$ctl00$DateTimeField$DateTimeFieldDate” …

    Any suggestions?

  12. VT Brad Says:

    Gaurav,

    I had the same problem and was able to fix it by forcing a client-side validation. Just set the OnValueChangeClientScript property to “Page_ClientValidate(\”\”);”

    Hope this helps,

    Brad

  13. novolocus.com » Notes on the Microsoft.SharePoint.WebControls.DateTimeControl Says:

    [...] It has it’s own validation, and doesn’t play nicely with standard ASP.NET validators – but can be made to do so. [...]

  14. Koen Zomers Says:

    Genious!

    For people who want to use the DateTimeControl within an InputFormSection, beware that you must encapsulate it within an InputFormControl in order to get the styling right. Also make sure you put the validators within the Templace_Control of the InputFormControl or else the trick described in this post won’t work.

    For example:

  15. Koen Zomers Says:

    Appearantly this forum can’t handle HTML tags in postings too well. Another attempt for the example:

    >wssuc:InputFormSection runat=”server” ID=”MyDateSection” Title=”Enter your date” Description=”This is the place where you can enter a date”<
    >template_inputformcontrols<
    >wssuc:InputFormControl runat=”server”<
    >Template_Control<
    >SharePoint:DateTimeControl ID=”MyDatePicker” runat=”server” DateOnly=”true” /<
    >asp:CompareValidator ID=”MyDateCompareValidator” runat=”server” ControlToValidate=”MyDatePicker$MyDatePickerDate” Type=”Date” Operator=”DataTypeCheck” ErrorMessage=”Invalid date” /<
    >/Template_Control<
    >/wssuc:InputFormControl<
    >/template_inputformcontrols<
    >/wssuc:InputFormSection<

  16. Koen Zomers Says:

    Almost right. Last attempt:

    <wssuc:InputFormSection runat=”server” ID=”MyDateSection” Title=”Enter your date” Description=”This is the place where you can enter a date”>
    <template_inputformcontrols>
    <wssuc:InputFormControl runat=”server”>
    <Template_Control>
    <SharePoint:DateTimeControl ID=”MyDatePicker” runat=”server” DateOnly=”true” />
    <asp:CompareValidator ID=”MyDateCompareValidator” runat=”server” ControlToValidate=”MyDatePicker$MyDatePickerDate” Type=”Date” Operator=”DataTypeCheck” ErrorMessage=”Invalid date” />
    </Template_Control>
    </wssuc:InputFormControl>
    </template_inputformcontrols>
    </wssuc:InputFormSection>


Leave a Reply