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
Related
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>
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.
In my asp.net application, I've tried to use validation summary in my web page.I've controls like
<td>First Name *</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="reqValStaffFirstName" runat="server"
ErrorMessage="Enter FirstName" Text="*"
ControlToValidate="txtFirstName" Display="Dynamic"
ValidationGroup="valGrpStaffGeneral">
</asp:RequiredFieldValidator>
</div>
</td>
<td>Last Name *</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
<div>
<asp:RequiredFieldValidator ID="reqValStaffLastName" runat="server"
ErrorMessage="Enter LastName" Text="*"
ControlToValidate="txtLastName" Display="Dynamic"
ValidationGroup="valGrpStaffGeneral">
</asp:RequiredFieldValidator>
</div>
</td>
<td>
<asp:Button Id="btnSave" runat="server" ValidationGroup="valGrpStaffGeneral">
<td>
and my validationsummary script is
<asp:ValidationSummary ID="summary1" ShowSummary="true" runat="server"
ValidationGroup="valGrpStaffGeneral" DisplayMode="BulletList" />
If I click on save button without filling anything in textbox1 and textbox2 its showing validation errors in my validation summary area. If I fill anything in my textboxes and press tab the error(*) disappears automatically, but the error in the validation summary not changing automatically and its displaying until I click on save button, what should I do to make the validation summary change the fixed errors automatically.
This might be of help - look at the EnableClientScript property of the ValidationSummary control.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.enableclientscript.aspx
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;
}
}
I want to use maskededit to make the user to do the same as in the ajax control toolkit sample for maskededit, but I need to use another format, since dates at my client is displayed as dd-MM-yyyy. I also need to put a value in there to begin with, since the date is loaded from a db (once I get this code working, that is)
Look at this sample:
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/maskededit/maskededit.aspx
I need to do the same as in the textbox labeled "Enter Date (format: 99/99/9999): "
I downloaded the sample code and changed the format to "99-99-9999" in the relevant code:
But the sample date "12-12-1999" is not displayed in the textbox. Why not?
I cant figure out, what I am doing wrong. I also tried setting the cultureName to da-DK, but it did not chance anything. I also tried Chrome, Firefox and IE7, same behaviour...
How do I fix this?
<asp:TextBox ID="TextBox5" runat="server" Width="130px" MaxLength="1" style="text-align:justify" ValidationGroup="MKE">12-12-1999</asp:TextBox>
<asp:ImageButton ID="ImgBntCalc" runat="server" ImageUrl="~/images/Calendar_scheduleHS.png" CausesValidation="False" />
<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender5" runat="server"
TargetControlID="TextBox5"
Mask="99-99-9999"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
MaskType="Date"
DisplayMoney="Left"
AcceptNegative="Left"
ErrorTooltipEnabled="True" />
<ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator5" runat="server"
ControlExtender="MaskedEditExtender5"
ControlToValidate="TextBox5"
EmptyValueMessage="Date is required"
InvalidValueMessage="Date is invalid"
Display="Dynamic"
TooltipMessage="Input a date"
EmptyValueBlurredText="*"
InvalidValueBlurredMessage="*"
ValidationGroup="MKE" />
I suposse you'll have this already fixed, but ... have you tried using UserDateFormat="DayMonthYear" ?
UserDateFormat="DayMonthYear"
<asp:TextBox ID="TextBox5" runat="server" Width="130px" MaxLength="1" style="text-align:justify" ValidationGroup="MKE">12-12-1999</asp:TextBox>
<asp:ImageButton ID="ImgBntCalc" runat="server" ImageUrl="~/images/Calendar_scheduleHS.png" CausesValidation="False" />
<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender5" runat="server"
TargetControlID="TextBox5"
Mask="99-99-9999"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
MaskType="Date"
DisplayMoney="Left"
AcceptNegative="Left"
ErrorTooltipEnabled="True" **UserDateFormat="DayMonthYear"** />
<ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator5" runat="server"
ControlExtender="MaskedEditExtender5"
ControlToValidate="TextBox5"
EmptyValueMessage="Date is required"
InvalidValueMessage="Date is invalid"
Display="Dynamic"
TooltipMessage="Input a date"
EmptyValueBlurredText="*"
InvalidValueBlurredMessage="*"
ValidationGroup="MKE" />
I use ValidationExpression in MaskedEditValidator replace userDateFormat type Date dd/MM/yyy is very good.
This is :
ValidationExpression="(((((0[1-9])|(1\d)|(2[0-8]))\/((0[1-9])|(1[0-2])))|((31\/((0[13578])|(1[02])))|((29|30)\/((0[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))"
And in MaskedEditExtender put MaskType="none".
Validate ok.
<asp:TextBox ID="TextBox5" runat="server" Width="130px" MaxLength="10" style="text-align:justify" ValidationGroup="MKE" Text="12-12-1999"></asp:TextBox>
I am wondering if you move the text to the actual text attribute, if that would make a difference... I don't have the most recent version of AjaxToolkit other wise I would try it my self... Oh I just noticed is your max length really set to one or is that a copy and paste error? That would cause problems too.