Compare Validator not working for ajax Calendar Extender - asp.net

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.

Related

ASP.NET validator never validates as true

The code below has always worked, but now somehow I can't submit the form anymore even though I provide both username and password. I get no errors in backend or the Chrome Developer Console. I tried enabling/disabling all validators and found that if I add Enabled="false" to validator UserNameRequired, the form does submit. But obviously I want to have this requiredfieldvalidator enabled.
Why is this happening?
You can also see it live here (fill in a random email address/password): http://www.zorgbeurs.nl/login
<table width="300">
<tr>
<td>
<asp:Label ID="UserNameLabel" Text="Email" runat="server" AssociatedControlID="UserName"/>
</td>
<td>
<asp:TextBox ID="UserName" Text="" runat="server" Width="160px"/><br />
<asp:RequiredFieldValidator Enabled="false" ValidationGroup="loginuser" Display="Dynamic" SetFocusOnError="True" ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="*"/>
<asp:RegularExpressionValidator ControlToValidate="UserName" ValidationGroup="loginuser" ErrorMessage="Ongeldig emailadres" ValidationExpression="<%$resources:glossary,regexValidEmail %>" ID="revUsername" runat="server" Display="Dynamic"/>
</td>
</tr>
<tr>
<td valign="top">
<asp:Label ID="PasswordLabel" Text="<%$ Resources:Glossary,Password%>" runat="server" AssociatedControlID="Password"/>
</td>
<td valign="top">
<asp:TextBox ID="Password" runat="server" TextMode="Password" Width="160px"/><br />
<asp:RequiredFieldValidator Enabled="false" SetFocusOnError="True" Display="Dynamic" ValidationGroup="loginuser" ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="*" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:ImageButton CausesValidation="true" ValidationGroup="loginuser" ID="LoginMember" meta:resourcekey="ibtnLogin" CommandName="Login" runat="server"/>
<asp:CheckBox TextAlign="Right" CssClass="cblist" Font-Bold="false" ID="RememberMe" runat="server" Text="<%$ Resources:Glossary,loginrememberme%>" /><br />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="FailureText" runat="server" ForeColor="Red" Font-Bold="true" EnableViewState="False"/>
</td>
</tr>
</table>
update
I also tried adding the ValidationExpression of revUsername expression directly in the code [-+.'\w]+#[-.\w]+\.[-.\w]+, but that does not help.
I can see the required validators working in your live site without making a postback.
Regarding the email's regular expression validator, it seems that your expression is not working properly. So, I suggest you to change your regular expression, try this one:
<asp:RegularExpressionValidator Enabled="true" ControlToValidate="UserName"
ValidationGroup="loginuser" ErrorMessage="Ongeldig emailadres"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ID="revUsername" runat="server" Display="Dynamic" />
Also, remember to enable it again in order to make it work both on client and server side.
Hope it can help you!

2 validators validate one at a time

Hi I have a change password page.
I have 2 textboxes, New Password and Confirm Password.
Both have a requiredFieldValidators.
And a compareValidator.
But what happens is when I key in the first field New Password, the second textbox's requiredFieldValidator and CompareValidator are showing error messages at the same time.
I only want to show requiredFieldValidator and only when that validator returns true, I want to validate compareValidator.
How can I achieve that. The following is my code.
<table class="editAccTable">
<tr>
<td>
New Password<br />
<asp:TextBox ID="txtNewPwd" runat="server" TextMode="Password" Width="204px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="rfvNewPwd" runat="server"
ControlToValidate="txtNewPwd"
ValidationGroup="vgChangePwd"
Display="Dynamic"
></asp:RequiredFieldValidator>
</td>
<td>
Confirm New Password<br />
<asp:TextBox ID="txtConfPwd" runat="server" TextMode="Password" Width="204px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="rfvConfPwd" runat="server"
ControlToValidate="txtConfPwd"
ValidationGroup="vgChangePwd"
Display="Dynamic"
></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvChangePwd" runat="server"
ValidationGroup="vgChangePwd"
ControlToValidate="txtNewPwd"
ControlToCompare="txtConfPwd"
ValueToCompare="String"
Display="Dynamic"
></asp:CompareValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:ImageButton ID="ibtnPwdChange" runat="server"
ImageUrl="/images/submit.jpg" onclick="ibtnPwdChange_Click"
ValidationGroup="vgChangePwd"
Width="90px" Height="36px"
/>
<br />
<asp:Label ID="lblSuccessPwdChange" runat="server"></asp:Label>
</td>
</tr>
</table>
There is a couple of work around.
For my case, I can remove the requiredFieldValidator for the second textbox as it is enough to have only compareValidator.
But there will be some cases, that there must have 2 validators. In that case, we can use css to overlap the 2 validators.
Try CompareValidator like this
<asp:CompareValidator id="cvChangePwd" runat="server" ControlToValidate="txtConfPwd" ControlToCompare="txtNewPwd" ErrorMessage="Your passwords do not match up!" Display="Dynamic" />
set display property as dynamic
display="dynamic" in all the validators

compareValidator Compare to textboxes with dates

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;
}
}

ASP.NET - Required Field Validator giving false negative errors on Listbox

Background: I have a bundles listbox that inherits values from the carriers listbox once a carrier is selected via a web service.
I have a validationGroup on the button, I used Page.IsValid on the click handler and it says "Nothing".
When i select different carriers in IE8 it resets the other form values but not in IE9.
With Autopost=false on the lbCarriers, the Bundles listbox wont load any data.
With CausesValidation="true" in "lbCarriers", Bundles listbox wont load any data either Do you know how to do that w Ajax?
Do you know how I could do this w/ Ajax?
Problem: Using the required field validator on the bundles listbox is returning an a false erorr when I have bundles selected. When I click the Send Button, I get the "Select At Least 1 Bundle" Error Message but the invitation still sends out an i get an email.
Here's a screenshot of the application:
asp.net code on default.aspx page:
<tr>
<td class="style5">
Carrier:<br />
<font size="1">*Hold Ctrl Key Down to Select Multiple Carriers</font></td>
<td bgcolor="#ffffff" class="style7">
<asp:ListBox ID="lbCarriers" SelectionMode="Multiple" AutoPostBack="true"
runat="server" Height="86px" Width="250px" ValidationGroup="ValidationGroup">
</asp:ListBox>
</td>
<td bgcolor="#ffffff" class="style2">
<asp:RequiredFieldValidator ID="CarrierValidator" runat="server" Text="*"
ErrorMessage="Select At Least 1 Carrier" ControlToValidate="lbCarriers"
ValidationGroup = "ValidationGroup" ForeColor="Red" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style1">
Bundles:<br />
<font size="1">*Hold Ctrl Key Down to Select Multiple Bundles</font></td>
<td bgcolor="#ffffff" class="style6">
<asp:ListBox ID="bundles" SelectionMode="Multiple" runat="server" Height="86px"
Width="250px" Enabled="True"
ValidationGroup="ValidationGroup" CausesValidation="True">
</asp:ListBox>
</td>
<td bgcolor="#ffffff" class="style2">
<asp:RequiredFieldValidator ID="BundleValidator" runat="server" Text="*"
ErrorMessage="Select At Least 1 Bundle" ControlToValidate="bundles"
ValidationGroup = "ValidationGroup" ForeColor="Red" ></asp:RequiredFieldValidator>
</td>
</tr>
<asp:Button ID="Send_Button" runat="server"
Text="Send Invitation" ValidationGroup="ValidationGroup" Width="123px"/>
<br />
<asp:Label ID="Send_Success" runat="server" Text="Invitation sent!" Visible="false"></asp:Label>
<br />
<asp:ValidationSummary ID="ValidationSummary" runat="server" ForeColor="Red"
ValidationGroup="ValidationGroup" />
Question: What alternate code or work-around do you recommend for this issue?
Thanks for looking!
EDIT:
Add CausesValidation="true" in "lbCarriers"
I have removed Autopost="true" from first listbox i.e. "lbCarriers" and it is working now.

Rangevalidator for calendar extender entries gives problems in safari

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

Resources