Link to home
Start Free TrialLog in
Avatar of Sandeep Sood
Sandeep SoodFlag for India

asked on

asp.Net date comparison using calendar extender and compare validator

An employee is filling leave application form, mentions leave start date and end date. I have following on my .aspx form :

 
Label(Leave starts Date), text box, calendar extender (with format set to dd/mm/yyyy), required field validator


Label(Leave End Date), text box, calendar extender (with format set to dd/mm/yyyy), required field validator

Compare Validator
to ensure Start Date is not less than end date.



      <div class="form-group">
            <label>Leave starts Date</label>
            <asp:TextBox runat="server" ID="txtDurationFrom" CssClass="form-control" AutoPostBack="true" />
            <cc1:CalendarExtender ID="txtDurationFrom_CalendarExtender" runat="server" TargetControlID="txtDurationFrom"  Format="dd/MM/yyyy" > 
            </cc1:CalendarExtender>

            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Starting Date of Leave Period" ControlToValidate="txtDurationFrom" >
            </asp:RequiredFieldValidator>
 

            <label>Leave End Date</label>
            <asp:TextBox runat="server" ID="txtDurationTo" CssClass="form-control" AutoPostBack="true" />
            <cc1:CalendarExtender ID="txtDurationTo_CalendarExtender" runat="server" Enabled="True"
            TargetControlID="txtDurationTo" Format="dd/MM/yyyy" >
            </cc1:CalendarExtender>

            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Enter Last Date of Leave Period"
            ControlToValidate="txtDurationTo">
            </asp:RequiredFieldValidator>
   
 
            <asp:CompareValidator  ID="cmpEndDate" runat="server" ErrorMessage="Sorry, Date range is not valid."
            ControlToCompare="txtDurationFrom" ControlToValidate="txtDurationTo" ValueToCompare="txtDurationFrom"
            Operator="GreaterThanEqual" Type="Date" ValidationGroup="NameOfValidationGroup" Font-Bold="True" CultureInvariantValues="True"></asp:CompareValidator>
   

      </div>



Problem : when i enter date such that month is smaller than 10, it works.
in other cases, compare validatr starts showing error messagem ( Sorry, Date range is not valid)


Where am i wrong. What is the solution.

Thanks.
Avatar of HainKurt
HainKurt
Flag of Canada image

remove below from asp:CompareValidator

ValueToCompare="txtDurationFrom"

Open in new window


and add

EnableClientScript="true"

Open in new window


also remove autopostback="true" from dates...

+ on button save click or post back, use

cmpEndDate.Validate()
If cmpEndDate.IsValid Then
  ... do something...
End If

Open in new window


if you also need to validate on server side...
here is a working sample for type integer (I dont have calendar, so I used integer type validator)

<label>Leave starts Date</label>
<asp:TextBox runat="server" ID="txtDurationFrom" CssClass="form-control" />

<label>Leave End Date</label>
<asp:TextBox runat="server" ID="txtDurationTo" CssClass="form-control" />

 <asp:CompareValidator ID="cmpEndDate" runat="server" ErrorMessage="Sorry, Date range is not valid." EnableClientScript="true"
     ControlToCompare="txtDurationFrom" ControlToValidate="txtDurationTo" Operator="GreaterThanEqual" Type="Integer" ValidationGroup="NameOfValidationGroup" Font-Bold="True" CultureInvariantValues="True"></asp:CompareValidator>

<asp:Button runat="server" ID="btnSave" Text="Save" />

Open in new window


    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        cmpEndDate.Validate()
        If cmpEndDate.IsValid Then
          ' do something
        End If
    End Sub

Open in new window

Avatar of Sandeep Sood

ASKER

Thanks HainKurt. but i need a solution with calendar  extender only.
ASKER CERTIFIED SOLUTION
Avatar of Sandeep Sood
Sandeep Sood
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.