How to validate an optional date with a date input mask

1 Answer 22 Views
Date/Time Pickers
Lee
Top achievements
Rank 1
Iron
Iron
Iron
Lee asked on 15 Nov 2021, 06:58 PM

I have a dateinput field that I need to validate. Unfortunately I can't seem to figure out how to make it work. The requirements are as follows:

1. The date is optional.

2. If the date is provided, it must be a valid date.

3. The input mask could be in various languages.

When I try to validate I get invalid date when the date field just has the input mask (month/day/year) showing. 

Here is a dojo showing my example.

https://dojo.telerik.com/@dojolee/ukIKedUL

1 Answer, 1 is accepted

Sort by
0
Georgi Denchev
Telerik team
answered on 18 Nov 2021, 01:55 PM

Hello, Lee,

Thank you for the provided Dojo and details.

The parseDate method will return null if the provided value is invalid. However in this case you also need to allow empty values, so checking if the input has null value will not help.

My suggestion would be for you to declare a global variable that holds the initial(empty/mask) value of the datepicker. After that in the validation section, check if the current value matches the mask. If it does, the input is empty and an error should not be thrown. If it doesn't, the input has a value and the kendo.parseDate method will validate if it is correct or not.

Updated Dojo example:

https://dojo.telerik.com/@gdenchev/UMEVUfUR 

Let me know if you have any questions.

Best Regards,
Georgi Denchev
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Date/Time Pickers
Asked by
Lee
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Georgi Denchev
Telerik team
Share this question
or