This is a migrated thread and some comments may be shown as answers.

Custom validator for DateTimePicker

4 Answers 298 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
Antonin Jelinek
Top achievements
Rank 1
Antonin Jelinek asked on 04 Jan 2010, 03:10 PM
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



4 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 06 Jan 2010, 10:57 AM
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.
0
Uday Anthony
Top achievements
Rank 1
answered on 20 Jan 2012, 08:43 PM
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
0
Princy
Top achievements
Rank 2
answered on 23 Jan 2012, 01:19 PM
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.
0
Uday Anthony
Top achievements
Rank 1
answered on 19 Feb 2012, 08:17 PM
Thanks Princy. Works like a charm.
Tags
Calendar
Asked by
Antonin Jelinek
Top achievements
Rank 1
Answers by
Mira
Telerik team
Uday Anthony
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or