Sandeep Sood
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_Calend arExtender " runat="server" TargetControlID="txtDurati onFrom" Format="dd/MM/yyyy" >
</cc1:CalendarExtender>
<asp:RequiredFieldValidato r ID="RequiredFieldValidator 1" runat="server" ErrorMessage="Enter Starting Date of Leave Period" ControlToValidate="txtDura tionFrom" >
</asp:RequiredFieldValidat or>
<label>Leave End Date</label>
<asp:TextBox runat="server" ID="txtDurationTo" CssClass="form-control" AutoPostBack="true" />
<cc1:CalendarExtender ID="txtDurationTo_Calendar Extender" runat="server" Enabled="True"
TargetControlID="txtDurati onTo" Format="dd/MM/yyyy" >
</cc1:CalendarExtender>
<asp:RequiredFieldValidato r ID="RequiredFieldValidator 2" runat="server" ErrorMessage="Enter Last Date of Leave Period"
ControlToValidate="txtDura tionTo">
</asp:RequiredFieldValidat or>
<asp:CompareValidator ID="cmpEndDate" runat="server" ErrorMessage="Sorry, Date range is not valid."
ControlToCompare="txtDurat ionFrom" ControlToValidate="txtDura tionTo" ValueToCompare="txtDuratio nFrom"
Operator="GreaterThanEqual " Type="Date" ValidationGroup="NameOfVal idationGro up" Font-Bold="True" CultureInvariantValues="Tr ue"></asp: CompareVal idator>
</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.
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_Calend
</cc1:CalendarExtender>
<asp:RequiredFieldValidato
</asp:RequiredFieldValidat
<label>Leave End Date</label>
<asp:TextBox runat="server" ID="txtDurationTo" CssClass="form-control" AutoPostBack="true" />
<cc1:CalendarExtender ID="txtDurationTo_Calendar
TargetControlID="txtDurati
</cc1:CalendarExtender>
<asp:RequiredFieldValidato
ControlToValidate="txtDura
</asp:RequiredFieldValidat
<asp:CompareValidator ID="cmpEndDate" runat="server" ErrorMessage="Sorry, Date range is not valid."
ControlToCompare="txtDurat
Operator="GreaterThanEqual
</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.
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" />
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
ASKER
Thanks HainKurt. but i need a solution with calendar extender only.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks.
Open in new window
and add
Open in new window
also remove autopostback="true" from dates...
+ on button save click or post back, use
Open in new window
if you also need to validate on server side...