Custom validator for DateTimePicker

5 posts, 0 answers
  1. Antonin Jelinek
    Antonin Jelinek avatar
    5 posts
    Member since:
    Jul 2005

    Posted 04 Jan 2010 Link to this post

    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



  2. Mira
    Admin
    Mira avatar
    1124 posts

    Posted 06 Jan 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Uday Anthony
    Uday Anthony avatar
    5 posts
    Member since:
    Oct 2009

    Posted 20 Jan 2012 Link to this post

    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
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 Jan 2012 Link to this post

    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.
  6. Uday Anthony
    Uday Anthony avatar
    5 posts
    Member since:
    Oct 2009

    Posted 19 Feb 2012 Link to this post

    Thanks Princy. Works like a charm.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017