Validate end date should be greater than start date? - asp.net

datePicker.ascx
<asp:TextBox ID="txtDatePicker" runat="server"></asp:TextBox>
<asp:ImageButton runat="Server" ID="Image1" ImageUrl="~/Images/calendar.png" AlternateText="Click to show calendar" />
<ajaxToolkit:CalendarExtender Format="dd/MM/yyyy" ID="CalendarExtender1" TargetControlID="txtDatePicker" runat="server" PopupButtonID="Image1" />
register.aspx
<%# Register Src="~/Modules/datePicker.ascx" TagName ="datePicker" TagPrefix ="uc1" %>
Start Date <uc1:datePicker ID="datePickerSD" runat="server" >
End Date <uc1:datePicker ID="datePickerED" runat="server" />
<asp:CustomValidator ID="CustomValidator1" ControlToValidate="datePickerED"
ErrorMessage="End date should be greater than start date" runat="server" />
Tried to get clientid of start date..but confused..

<asp:CompareValidator ID="cmpr" runat="server" ControlToCompare ="txtDate1" ControlToValidate="txtDate2" Operator="GreaterThan" Type="Date" ErrorMessage="To date should great than small data"></asp:CompareValidator>
Have you tried compare validator ? This can help you

Solved :
<asp:CompareValidator ID="compareValidatorDate" runat="server" ControlToValidate="datePickerED$txtDatePicker" ControlToCompare ="datePickerSD$txtDatePicker" CultureInvariantValues="true"
ErrorMessage="End date should be greater than start date" Operator="GreaterThanEqual" SetFocusOnError="false" Type="String" Display="Dynamic" EnableClientScript="true" Font-Size="Small" ForeColor="Red"></asp:CompareValidator>

Related

How to make a extender calendar unable to select a date base on a selected date?

How can I make user unable to select an end date before the an start date?
<asp:Label ID="lblStartDate" runat="server" Text="Start Date: " CssClass="labelClass"></asp:Label>
<asp:TextBox ID="tbStartDate" runat="server" ReadOnly="True"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalExtStartDate" runat="server" Format="dd/MM/yyyy" TargetControlID="tbStartDate" />
<asp:Label ID="lblEndDate" runat="server" Text="End Date: " CssClass="labelClass"></asp:Label>
<asp:TextBox ID="tbEndDate" runat="server" ReadOnly="True"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalExtEndDate" runat="server" TargetControlID="tbEndDate" Format="dd/MM/yyyy" />
In my .aspx.cs I've got but it still doesn't work.
if (!IsPostBack)
{
CalExtStartDate.StartDate = DateTime.Now.AddDays(-7);
CalExtEndDate.StartDate = CalExtStartDate.SelectedDate;
}
I believe that you can use a compare validator paired with 2 required field validators (one for each date textbox):
<asp:TextBox ID="tbStartDate" runat="server" ReadOnly="True"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="End Date required" ID="requiredDate1" ControlToValidate="tbStartDate" />
<asp:TextBox ID="tbEndDate" runat="server" ReadOnly="True"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="Start Date required" ID="requiredDate2" ControlToValidate="tbEndDate" />
<asp:CompareValidator runat="server" ID="compareValidator1" ErrorMessage="End date must be after start date" ControlToCompare="tbStartDate" ControlToValidate="tbEndDate" Operator="GreaterThan" Type="Date" />

user control (.ascx) with a textbox and a ajax calendar extender - end date should be greater than start date

I have a user control (.ascx) with textbox and ajax calendar extender . I used this twice for start date and end date in web forms. How to compare these dates using compare validator control?
my code:
.ascx
<asp:TextBox ID="txtDatePicker" runat="server"></asp:TextBox>
<asp:ImageButton runat="Server" ID="Image1" ImageUrl="~/Images/calendar.png" AlternateText="Click to show calendar" />
<ajaxToolkit:CalendarExtender Format="dd/MM/yyyy" ID="CalendarExtender1" TargetControlID="txtDatePicker" runat="server" PopupButtonID="Image1" />
.aspx
Start Date <uc1:datePicker ID="datePickerSD" runat="server" >
End Date <uc1:datePicker ID="datePickerED" runat="server" />
<asp:CompareValidator ID="compareValidatorDate" runat="server" ControlToValidate="datePickerED.UniqueID" ControlToCompare="datePickerSD.ClientID" CultureInvariantValues="true"
ErrorMessage="End date should be greate than start date" Operator="GreaterThanEqual" SetFocusOnError="true" Type="Date"> </asp:CompareValidator>
getting error:
you should use custom validator in which you specify javascript function that should be executed upon validation. in this function u can do whatever logic u want.
<asp:CustomValidator ID="custDateOfBirth" runat="server" ControlToValidate="txtBirthDate" ClientValidationFunction="CheckDate" ErrorMessage="Enter less than the current date" CssClass="ClsRedtext" SetFocusOnError="true" Type="Date" ></asp:CustomValidator>
Solved:
<asp:CompareValidator ID="compareValidatorDate" runat="server" ControlToValidate="datePickerED$txtDatePicker" ControlToCompare ="datePickerSD$txtDatePicker" CultureInvariantValues="true"
ErrorMessage="End date should be greater than start date" Operator="GreaterThanEqual" SetFocusOnError="false" Type="String" Display="Dynamic" EnableClientScript="true" Font-Size="Small" ForeColor="Red"></asp:CompareValidator>

asp.net "compare validator" validation

I am trying to validate fromdate & todate textboxes in asp.net using compare validator
my script is:
<table><tr><td>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Label ID="Label1" runat="server" Text="Fromdate:"> </asp:Label>
<asp:TextBox ID="fromdatetxt" runat="server" Height="21px" Width="103px" ></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="fromdatetxt_CalendarExtender" runat="server"
Enabled="True" TargetControlID="fromdatetxt">
</ajaxToolkit:CalendarExtender>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text="Todate:"></asp:Label>
<asp:TextBox ID="todatetxt" runat="server" Height="21px" Width="105px" ></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="todatetxt_CalendarExtender" runat="server"
Enabled="True" TargetControlID="todatetxt">
</ajaxToolkit:CalendarExtender>
</td>
<asp:CompareValidator ID="CompareValidatorDate" runat="server" ControlToCompare="todatetxt"
ControlToValidate="fromdatetxt" Display="None" ErrorMessage="From date cannot be greaterthan To date"
operator = "LessThanEqual" Type="Date" ValidationGroup="DateValidation"></asp:CompareValidator>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" ValidationGroup="DateValidation" />
<asp:ValidationSummary ID="ValidationSummaryDate" ShowMessageBox="true" ShowSummary="False"
ValidationGroup="DateValidation" runat="server" /></td></tr></table>
</asp:Panel>
This is working fine! but I'm getting message box only when click the button. But I want to get message box the moment I clicked date in Todate in calendar control, and the textboxes must get clear. please help me out .
You can solve your problem using "Page_ClientValidate" function of javascript and "OnClientDateSelectionChanged" event of CalendarExtender.
You do not need to change your CompareValidator i.e.
<asp:CompareValidator ID="CompareValidatorDate" runat="server" ControlToCompare="todatetxt"
ControlToValidate="fromdatetxt" Display="None" ErrorMessage="From date cannot be greater than To date"
Operator="LessThanEqual" Type="Date" ValidationGroup="DateValidation"></asp:CompareValidator>
You need to add OnClientDateSelectionChanged event to your CalendarExtender as
<ajaxtoolkit:CalendarExtender id="todatetxt_CalendarExtender" runat="server" enabled="True"
targetcontrolid="todatetxt" OnClientDateSelectionChanged="validate" >
"validate" here is a javascript function. In that function you need to use "Page_ClientValidate" method of javascript as
var validate = function () {
var isValid = Page_ClientValidate("DateValidation"); //parameter is the validation group
if (!isValid) {
$("#<%= todatetxt.ClientID %>").val(''); //jquery to clear the textbox
}
}
You can modify the "validate" function as per your convenience.
I hope this helped.
Regards,
Samar
add Display="Dynamic" in validator
<asp:CompareValidator ID="CompareValidatorDate" runat="server" ControlToCompare="todatetxt"
ControlToValidate="fromdatetxt" Display="Dynamic" ErrorMessage="From date cannot be greaterthan To date"
Operator="LessThanEqual" Type="Date" ValidationGroup="DateValidation"></asp:CompareValidator>

date validation in asp.net

is this following a proper date validation fn
<asp:TextBox ID="date" Width="80px" MaxLength="10" runat="server" />
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="date"
ErrorMessage="date. " ValidationExpression="^(\d{4})(\d{2})(\d{2})$" Display="Static" Font-Names="Arial"
Font-Size="11" runat="server">
enter a valid date formate
</asp:RegularExpressionValidator>
I believe this would be a better approach, use what's built-in:
<asp:TextBox ID="date" Width="80px" MaxLength="10" runat="server" />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="date" ErrorMessage="* Enter a valid date"
Operator="DataTypeCheck" Type="Date" />
The key settings are Operator and Type.
Note if you have to consider multiple cultures and such, this becomes a much more involved question, hopefully that isn't the case.

Asp.Net End date greater than start date

Work on Asp.Net vs 08.
Below is my code
<asp:TextBox ID="txtSTART_DATE" runat="server"></asp:TextBox>
<cc1:PopupControlExtender ID="txtSTART_DATE_PopupControlExtender" runat="server" CommitProperty="value"
CommitScript="e.value += '';" PopupControlID="pnlSTART_DATE"
Position="Bottom" TargetControlID="txtSTART_DATE">
</cc1:PopupControlExtender>
<asp:TextBox ID="txtEND_DATE" runat="server"
ontextchanged="txtEND_DATE_TextChanged"></asp:TextBox>
<cc1:PopupControlExtender ID="txtEND_DATE_PopupControlExtender" runat="server" CommitProperty="value"
CommitScript="e.value += '';" PopupControlID="pnlEND_DATE"
Position="Bottom" TargetControlID="txtEND_DATE">
</cc1:PopupControlExtender>
C# syntax is
protected void dtpSTART_DATE_SelectionChanged(object sender, EventArgs e)
{
AjaxControlToolkit.PopupControlExtender.GetProxyForCurrentPopup(this.Page).Commit(dtpSTART_DATE.SelectedDate.ToString("dd MMM yyyy"));//Set the value
}
protected void dtpEND_DATE_SelectionChanged(object sender, EventArgs e)
{
AjaxControlToolkit.PopupControlExtender.GetProxyForCurrentPopup(this.Page).Commit(dtpEND_DATE.SelectedDate.ToString("dd MMM yyyy"));//Set the value
}
want to compare start date with the end date.Verify that end date greater than start date and today date.How to check the value?
You could use the asp.net CompareValidator, e.g.
<asp:TextBox ID="txtSTART_DATE" runat="server" />
<asp:TextBox ID="txtEND_DATE" runat="server" />
<asp:CompareValidator ID="cmpDates" ControlToValidate="txtEND_DATE"
ControlToCompare="txtSTART_DATE" Operator="GreaterThan" Display="dynamic"
ErrorMessage="End date must be after start date" runat="server" />
This won't do a popup though, but if you are just after a validator then that should be ok.
The compare validator is actually very powerful, for instance, you can check the datatype of the argument as well, so I would use something like this:
<asp:TextBox ID="txtSTART_DATE" runat="server" />
<asp:CompareValidator ID="chkStartIsDate" runat="server" Display="Dynamic"
Operator="DataTypeCheck" Type="Date" ControlToValidate="txtSTART_DATE"
ErrorMessage="You must supply a valid start date" />
<asp:TextBox ID="txtEND_DATE" runat="server" />
<asp:CompareValidator ID="chkEndIsDate" runat="server" Display="Dynamic"
Operator="DataTypeCheck" Type="Date" ControlToValidate="txtEND_DATE"
ErrorMessage="You must supply a valid end date" />
<asp:CompareValidator ID="cmpStartAndEndDates" runat="server" Display="Dynamic"
Operator="GreaterThan" ControlToValidate="txtEND_DATE" ControlToCompare="txtSTART_DATE"
ErrorMessage="The end date must be after the start date" />
<asp:TextBox ID="txtStartDate" runat="server" />
<asp:TextBox ID="txtEndDate" runat="server" />
<asp:CompareValidator ID="cvStartEnd" Operator="GreaterThan" Type="Date"
ControlToValidate="txtEndDate" ControlToCompare="txtStartDate"
ErrorMessage="End date must be greater than start date!" runat="server"/>
Make sure to set property Type="Date" otherwise it would do a string compare and give u wrong results.

Resources