0

I'm using two RadDatePicker controls showing Date From and Date To and need to make sure that before submitting the form, at least one of the date is provided.

I have a JavaScript code checking to make sure that the user provides at least one date value:

function validateDates(sender, eventArgs) {

    var datePicker1 = $find("<%=RadDatePicker1.ClientID %>");
    var datePicker2 = $find("<%=RadDatePicker2.ClientID %>");


    if (datePicker1.get_selectedDate() == null && datePicker2.get_selectedDate() == null) 
    {
        alert("At least one date should be selected");
        eventArgs.set_cancel(true);
    }

       //alert(datePicker1.lastSetTextBoxValue.value);
       //return false;
}

That part works.

However, a user can also type a string in which case, it will also fails with the same message, because get_selecteddate() method also returns null.

But, what I want is to actually check if the value entered is of a date format and display the corresponding message to the user.

So, in my case, I would need to use a validation for the correct date format as well.

I noticed that when Telerik DatePicker is rendered, it builds the following html sections:

<input id="ctl00_Contentplaceholder2_RadDatePicker1_dateInput" name="ctl00$Contentplaceholder2$RadDatePicker1$dateInput" class="riTextBox riError" type="text" style="">

<input id="ctl00_Contentplaceholder2_RadDatePicker1_dateInput_ClientState" name="ctl00_Contentplaceholder2_RadDatePicker1_dateInput_ClientState" type="hidden" autocomplete="off" value="{&quot;enabled&quot;:true,&quot;emptyMessage&quot;:&quot;&quot;,&quot;validationText&quot;:&quot;&quot;,&quot;valueAsString&quot;:&quot;&quot;,&quot;minDateStr&quot;:&quot;1980-01-01-00-00-00&quot;,&quot;maxDateStr&quot;:&quot;2099-12-31-00-00-00&quot;,&quot;lastSetTextBoxValue&quot;:&quot;hhh&quot;}">

From that html I see that when a string is entered into a DatePicker, its input element gets assigned a css class called riError and its lastSetTextBoxValue has a string I entered.

So, I can assume, I can use those two properties to improve my validation logic.

How can I do that? Any ideas?

Sparky
  • 98,165
  • 25
  • 199
  • 285
gene
  • 2,098
  • 7
  • 40
  • 98
  • Can you attach some codepen/etc? :) – yossi Jul 08 '22 at 15:21
  • What do you mean? You need to see asp code for the controls? – gene Jul 08 '22 at 15:23
  • even tho you use server side to build it, the question is a pure client side issue. so, try to get it as a single html+js code, and use a codepen or similar service. why? because it's easier for us to "play with" and understand. also - try to give example values for the handling cases. Some of us has no experience with the specific tech (like me), but knows enough to solve it, it will make it easier.. and a nice gesture :) – yossi Jul 09 '22 at 11:19

0 Answers0