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

validate the selected date

2 Answers 190 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 01 Aug 2012, 04:10 PM
hi,
i wish to validate the contents of the datetime picker to ensure the date they selected is older than 18 years.. i.e for a date of birth, ensure the user is over 18 years old.
how do i add this validation layer to it? using the kendovalidator, it performs the validation on clicking the calender button which obviously wont work because the validated value is empty (or the previous value)

thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Iliana Dyankova
Telerik team
answered on 06 Aug 2012, 11:22 AM
Hello David,

I have already replied to your other forum thread on the same subject. For convenience I am pasting my reply here as well:

You could set a custom validation rules via the rules property of the Kendo UI Validator. For example: 

Copy Code
<div id="MyForm">
  <ul>
    <li>
       <input id="datepicker" required />
    </li>
  </ul>
</div>
 
<script>
   $(document).ready(function() {
      $("#datepicker").kendoDatePicker({});
     
      $("#MyForm").kendoValidator({
        rules: {
          custom: function(input){
            //debugger;
          }
        }
      })   
    });
</script>

Regarding the behavior you have observed ("the validation is triggered on clicking the date calendar button") - I tried to reproduce it, but to no avail. In order to assist you best please provide some more details or even best a small but runnable project which illustrates your scenario. Thank you in advance for your cooperation. 

Greetings,
Iliana Nikolova
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Atlas
Top achievements
Rank 1
answered on 08 Nov 2014, 12:48 AM
I have been pretty disgusted with the validation on the kendo datepicker. What good is validation if it doesn't work regardless of the way the data is input. The validation works great if you use the calendar icon to pick your date, but provides no validation if you hand type in a value.
I have been complaining about this for some time, and the solution I got from Telerik was a cluster. I was having to use a lot of extra code to make it work.
Well I finally got around to investigating custom code attributes, both server side and client side, and discovered that it is quite easy to add a custom code attribute that works on both the client and the server side. You have to create a class that handles both scenarios, and you have to write some custom javascript, but once you get the hang of it, it's really easy.
We have requirements on several of our date values that they are on or before today or on or after today, so I created two attributes:
1) DateTimeMaxNowAttribute
2) DateTimeMinNowAttribute
I will attach the files for all of this. One of the namespaces you will need is the one that supports: ModelClientValidationRule apparently, from what I am reading this used to be in a different namespace, but I you are using MVC 4 or later, this rule exists in the System.Web.WebPages namespace.
use the code  in the attached files and simply decorate your property like so:

        [DateTimeMaxNow]
        [DisplayName("Estoppel Request Date")]
        [DisplayFormat(DataFormatString = "{0:d}")]
        public DateTime? EstoppelRequestDate { get; set; }

Tags
Date/Time Pickers
Asked by
David
Top achievements
Rank 1
Answers by
Iliana Dyankova
Telerik team
Atlas
Top achievements
Rank 1
Share this question
or