Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > Calendar > Custom validator for DateTimePicker

Not answered Custom validator for DateTimePicker

Feed from this thread
  • Antonin Jelinek avatar

    Posted on Jan 4, 2010 (permalink)

    Hello,

    I have a standard DateTimePicker control and I'd like to have a custom validator (server side) against it, something like this:

    <telerik:RadDateTimePicker ID="picker1" runat="server" SelectedDate="<%#((DateTime)((DataRowView)Container.DataItem)[1])%>"  > 
      <DateInput DateFormat="d.M.yyyy H:mm" DisplayDateFormat="d.M.yyyy H:mm"  /> 
      <TimeView StartTime="10:0:0" EndTime="21:01:00" Interval="0:15:0" Columns="7" TimeFormat="HH:mm" Culture="cs-CZ" />  
    </telerik:RadDateTimePicker> 
                         
    <asp:CustomValidator ID="custValCasVykopu" runat="server" ControlToValidate="picker1" Display="dynamic" Text="Enter correct date and time" OnServerValidate="custValCasVykopu_Validate" /> 
                         

    and on the server:

    protected void custValCasVykopu_Validate(object source, ServerValidateEventArgs args) 
                string txtCasVykopu = args.Value; 
                DateTime casVykopu; 
                if (DateTime.TryParse(txtCasVykopu, out casVykopu)) 
                { 
                    args.IsValid = true
                } 
                else 
                { 
                    args.IsValid = false
                } 

    Now, the problem is that in this setup, args.value is looking like this: "2010-02-27-15-00-00" and this will not parse thru DateTime.TryParse (or at least I have not found a proper combination of culture and DateTimeStyles settings that will parse this string).

    My question is - is there any way to modify value property of DateTimePicker so it will produce string that will parse thru DateTime.TryParse ?

    Thanks,

    Antonin



    Reply

  • Mira Mira admin's avatar

    Posted on Jan 6, 2010 (permalink)

    Hi Antonin,

    Please use the SelectedDate property of the RadDateTimePicker directly in the ServerValidate event handler instead of the handler arguments to implement the desired functionality.
    I have followed your scenario and prepared a sample project for you showing how it is done.

    Additionally, I would like to recommend that you take a look at the Validation demo showing how validating the dates picked by the user can be done by using other standard ASP.NET validator controls.

    Greetings,
    Mira
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

    Reply

  • Uday Anthony avatar

    Posted on Jan 20, 2012 (permalink)

    Hello,

    I have the datetimepicker in an asp.net gridview and I need to perform the server validation using the custom validator. How would I validate in this scenario?

    Thanks,
    Uday

    Reply

  • Posted on Jan 23, 2012 (permalink)

    Hello,

    Try the following code snippet.
    C#:
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
      {
         CustomValidator cus=(CustomValidator)source;
         GridDataItem itm = (GridDataItem)cus.NamingContainer;
         RadDateTimePicker pkr = (RadDateTimePicker)itm.FindControl("RadDateTimePicker1");
         args.IsValid = false;
         if (pkr.SelectedDate.ToString()!="1/20/2012")
         {
             cus.ErrorMessage = "*";
         }
           
       }

    Thanks,
    Princy.

    Reply

  • Uday Anthony avatar

    Posted on Feb 19, 2012 (permalink)

    Thanks Princy. Works like a charm.

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > Calendar > Custom validator for DateTimePicker
Related resources for "Custom validator for DateTimePicker"

ASP.NET Calendar Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  |  Step-by-step Tutorial  ]