My compare validator is firing always even if it is greater and even if it is less.
<tr>
<td>
Selection Start Date:
</td>
<td>
<asp:TextBox ID="SelectionStartDateTextBox" runat="server"
Text='<%# Bind("SelectionStartDate") %>'></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="RequFilesStart"
ControlToValidate="SelectionStartDateTextBox"
ErrorMessage="Enter Selection Start date!" ValidationGroup="validation1">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
Selection End Date:
</td>
<td>
<asp:TextBox ID="SelectionEndDateTextBox" runat="server"
Text='<%# Bind("SelectionEndDate") %>'></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="ReqFiledEnd"
ControlToValidate="SelectionEndDateTextBox"
ErrorMessage="Enter Selection End date!" ValidationGroup="validation1">
</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValSelDate" runat="server"
ControlToValidate="SelectionEndDateTextBox"
ControlToCompare="SelectionStartDateTextBox"
CultureInvariantValues="true" Type="Date"
Operator="LessThanEqual" ValidationGroup="validation1"
ErrorMessage="Selection End Date should be greater then Selected start date"
Display="dynamic">
</asp:CompareValidator>
</td>
</tr>
I also have set it up Culture="en-GB" and have also set it up same on my web config.
I am using Ajax toolkit to display calendar attached to the TextBox
<cc1:calendarextender id="Calendarextendera3" runat="server" format="dd MMM yyyy"
targetcontrolid="SelectionEndDateTextBox">
</cc1:calendarextender>
<cc1:calendarextender id="Calendarextendera4" runat="server" format="dd MMM yyyy"
targetcontrolid="SelectionStartDateTextBox">
</cc1:calendarextender>
Please help I dont know what to do.
Thanks in advance
You should set
CultureInvariantValues="false"
in your CompareValidator. Too, the date format should be that of your current culture.
The CompareValidator seems not to be able to compare the format dd MMM yyyy. If you want to display this format, you can use a custom validator to compare the dates:
<asp:CustomValidator runat="server" ID="datesValidator" OnServerValidate="DatesValidator_Validate" ErrorMessage="end date should be greater than or equal to start date"></asp:CustomValidator>
protected void DatesValidator_Validate(object source, ServerValidateEventArgs args)
{
DateTime startDate = Convert.ToDateTime(SelectionStartDateTextBox.Text);
DateTime endDate = Convert.ToDateTime(SelectionEndDateTextBox.Text);
if (endDate < startDate)
{
args.IsValid = false;
}
}
Related
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" />
I want validation like, if i select Status as Reject then there must be some Comments(compulsory). and if status is Accept then comments may be blank (not compulsory)
How I will do it in ASP.NET, Please find my code
<tr>
<td width="30%">
<b>Status:</b>
</td>
<td>
<asp:RadioButton ID="lAccept" runat="server" AutoPostBack="True"
CausesValidation="True" Text="Accept" />
<asp:RadioButton ID="lReject" runat="server" AutoPostBack="True"
CausesValidation="True" Text="Reject " />
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ErrorMessage="Please Select it is Accepted or Rejected" ForeColor="Red"></asp:RequiredFieldValidator>
</tr>
<tr>
<td width="30%">
<b>Qty Rejected:</b>
</td>
<td>
<asp:TextBox ID="lRejectedQty" runat="server" CausesValidation="True"></asp:TextBox>
<%-- <asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Only interger between 1 to 10000000 " ondisposed="Page_Load"
oninit="Page_Load" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>--%>
<asp:RangeValidator ID="RangeValidator3" runat="server"
ErrorMessage="Rejected Quantity must be in change of 1 to 10,000,000"
ControlToValidate="lRejectedQty" Display="Dynamic" ForeColor="Red"
MaximumValue="10000000" MinimumValue="1"></asp:RangeValidator>
</td>
</tr>
<tr> <td width="30%">
<b>Comments:</b>
</td>
<td>
<TEXTAREA rows=5 cols=40 name="lComments" id="lComments"></TEXTAREA>
</td>
The simplest way for this is to use a CustomValidator.
MSDN: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator(v=vs.110).aspx
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="You must enter Comments if you choose to Reject."
OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
Then in your code-behind you may do more detailed checks for it being valid or not
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = true; // set it to be valid by default
if (lReject.Checked == true && string.IsNullOrWhiteSpace(lComments.Text) == true)
{
// Reject was selected and no comments were entered
e.IsValid = false;
}
}
The drawback is that this requires a PostBack to the server and will validate after the other types in most cases.
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>
I have two text boxes that accept dates.I have used a compare validator that checks whether the date entered in second text box >= date entered in first text box.
<table>
<tr>
<td>
<asp:Label runat="server" ID="lblDate" Width="200px" Text="Date Posted:"></asp:Label></td>
<td>
<asp:TextBox ID="txtDatePosted" runat="server" Width="200px" CausesValidation="true"></asp:TextBox>
<asp:Image ID="Image1" runat="server" Width="16px" AlternateText="" ImageUrl="~/Image/cal.jpg" Height="16px" />
<ajaxToolkit:CalendarExtender ID="calExtDatePost" runat="server" Format="dd/MM/yyyy" PopupButtonID="btnCalenderPopup" PopupPosition="TopRight" TargetControlID="txtDatePosted" />
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblExpiryDate" Width="200px" Text="Expiry Date:"></asp:Label></td>
<td>
<asp:TextBox ID="txtExpdate" runat="server" Width="200px" CausesValidation="true"></asp:TextBox>
<asp:Image ID="Image2" runat="server" Width="16px" AlternateText="" ImageUrl="~/Image/cal.jpg" Height="16px" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd/MM/yyyy" PopupButtonID="btnCalenderPopup" PopupPosition="TopRight" TargetControlID="txtExpdate" />
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Expiry date must be greater than Posted date" ControlToCompare="txtDatePosted" ControlToValidate="txtExpdate" Operator="GreaterThanEqual" ValidationGroup="vsNewUser" Type="Date" Text="*Expiry date must be greater than Posted date"></asp:CompareValidator>
</td>
</tr>
</table>
But the validator is not working. It is always showing the error message. Please help me.
I know the compare validator takes the below format:
1/1/2001
1-1-2001
5/4/2012
And these two text boxes accepts dates in the format like '27/07/2013'.
Issue is not in Ajax Control Toolkit.
Your code is not working because CompareValidator uses System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern property to determine how to convert string to date. CompareValidator supports ymd, mdy and dmy date formats.
And to fix your issue you need to explicitly specify Page.Culture property that has DateTimeFormatInfo.CurrentInfo.ShortDatePattern equal to dd/MM/yyyy. For example en-GB.
I have the following scenario:
arrival and departure dates have to be selected on a form, through 2 textboxes with a calendar extender each.
I validate the entries to check that no date before today is selected and to check that the departure is after the arrival. I used a rangevalidator and a comparevalidator.
In IE, Firefox and Opera it is working fine, in Safari (on windows) however both the validators go off even on entries that should be accepted.
It makes me suspect that the date format dd/MM/yyyy causes trouble for Safari.
(the dd/MMMM/yyyy also gave the same troubles in the other browsers, probably due to the dependency on UIculture)
The code is:
<tr>
<td>
<asp:TextBox ID="txtArrive" runat="server"></asp:TextBox>
<cc1:TextBoxWatermarkExtender ID="txtArriveWatermarkExt" runat="server"
Enabled="True" TargetControlID="txtArrive" WatermarkText="arrival date">
</cc1:TextBoxWatermarkExtender>
<cc1:CalendarExtender ID="ArriveCalendarExt" runat="server"
CssClass="MyCalendar" Enabled="True" Format="dd/MM/yyyy"
TargetControlID="txtArrive">
</cc1:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtArrive" ErrorMessage="please select arrival date"
Text="*" ValidationGroup="date" Display="none"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ErrorMessage="please make sure that your arrival date is later than today and not later than a year from now"
ControlToValidate="txtArrive" Type="Date" ValidationGroup="date" Text="*"></asp:RangeValidator>
</td>
<td>
<asp:TextBox ID="txtDepart" runat="server"></asp:TextBox>
<cc1:TextBoxWatermarkExtender ID="txtDepartWatermarkExt" runat="server"
Enabled="True" TargetControlID="txtDepart" WatermarkText="departure date">
</cc1:TextBoxWatermarkExtender>
<cc1:CalendarExtender ID="DepartCalendarExt" CssClass="MyCalendar" runat="server" Enabled="True"
Format="dd/MM/yyyy" TargetControlID="txtDepart">
</cc1:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtDepart" ErrorMessage="Please select departure date"
Text="*" ValidationGroup="date"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ControlToCompare="txtDepart" ControlToValidate="txtArrive"
ErrorMessage="your departure date has to be later than your arrival date"
Operator="LessThan" Type="Date" Text="*" ValidationGroup="date"></asp:CompareValidator>
</td>
<td>
</td>
</tr>
The range validator gets its values in code behind on Page_load
RangeValidator1.MinimumValue = DateTime.Now.AddDays(1).ToShortDateString();
RangeValidator1.MaximumValue = DateTime.Now.AddMonths(12).ToShortDateString();
Does anybody have any suggestions on how to solve this problem with safari?
set format property to Format="dd/MM/yyyy" of calender extender