date Compare Validator issue in asp.net - asp.net

I have a compare validator which validates on two <telerik:RadDatePicker> Start date and End date. The validation rule is simple, Check if Start Date is greater than the End Date and show the error message to the user to correct it
it works as expected but when the start and end dates are the same it is showing the message which is not expected. Code Below:
<asp:CompareValidator ID="dateCompareValidator" runat="server" ControlToValidate="endDate" ControlToCompare="startDate" Operator="GreaterThan" Type="Date" ErrorMessage="Start Date is greater than the End Date - please correct dates."Display="Dynamic"></asp:CompareValidator>
and the date pickers are as follows for both start date and end date:
<telerik:RadDatePicker CssClass="rcCalPopup" ID="endDate" runat="server"
Skin="Vista">
<DateInput ID="DateInput2" runat="server" LabelCssClass="radLabelCss_Vista" Skin="Vista">
</DateInput>
<Calendar ShowRowHeaders="false" ID="Calendar2" runat="server" UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False"
ViewSelectorText="x" Skin="Vista">
</Calendar>
<DatePopupButton CssClass="rcCalPopup"></DatePopupButton>
</telerik:RadDatePicker>

The rule you have defined with the Validator is:
Enddate must be greater than Startdate (note the missing equal)
The validator Operator property determines the rule for valid input, not invalid input.
So if you want to allow equal dates you have to use GreaterThanEqual
<asp:CompareValidator ID="dateCompareValidator" runat="server"
ControlToValidate="endDate" ControlToCompare="startDate"
Operator="GreaterThanEqual" Type="Date"
ErrorMessage="End date must be equal or greater than start date - please correct dates."Display="Dynamic">
</asp:CompareValidator>
Note that i've also changed the ErrorMessage accordingly. If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control.

Related

ASP: How to use a single span or div element to display validation errors

I have a field that required 3 different validators:
RequiredFieldValidator, RegularExpressionValidator, and CompareValidator.
Each validator has a "*" to show that something is wrong with the input:
<asp:TextBox ID="txtAddMin" runat="server" Width="30%" MaxLength="2"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="Min Value is missing" ControlToValidate="txtAddMin" ForeColor="Red" ValidationGroup="vgBinAdd">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator8" runat="server" ErrorMessage="Invalid Min Value Entered" ControlToValidate="txtAddMin" ForeColor="Red" ValidationGroup="vgBinAdd" ValidationExpression="^[0-9]{2}$">*</asp:RegularExpressionValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtAddMin" ControlToCompare="txtAddMax" Operator="LessThan" Type="Integer" ErrorMessage="Min Value shoule be less then MaxValue" ForeColor="Red" ValidationGroup="vgBinAdd">*</asp:CompareValidator>
Validations work as expected, however, when failing, the "" is displayed in different positions. So, when CompareValidator fails, the "" is displayed a few spaces from the input field.
Basically, every validator takes up some space after the input field and "*" is displayed with extra space before it based which validator fails.
IS that possible to fix that, so, the start is displayed in the fix position no matter which validator fails?

Check date is Greater using Calendar control in Asp.Net

I'm having a situation where i'm having a Calendar control in my page and when user selects a date it should check whether date selected is greater than today's date.
I tried a lot and Googled it as well, but in every place they are taking value using a textbox and i want the same to be done with a Calendar control.
Contrary to popular belief, you actually don't need any C# code to get this done! All you need is some extenders and validators on your date textbox. So let's say I had two textboxes -- one named txtStartDt and one named txtEndDate; this is how the extensions for them would be placed:
// Start Date
<asp:TextBox ID="txtStartDt" runat="server" CssClass="textboxint" /> // the main control itself
<ajax:CalendarExtender ID="calStartDt" runat="server" PopupPosition="BottomRight"
TargetControlID="txtStartDt" /> // allows the control to behave like a calendar picker
<asp:CompareValidator ID="CVStartDt" runat="server" Type="Date" Operator="LessThan"
ErrorMessage="Start Date must come before the End Date" ValidationGroup="ReportDate"
ControlToValidate="txtStartDt" ControlToCompare="txtEndDt" Display="None" /> // THIS is the main solution to your problem
<ajax:ValidatorCalloutExtender ID="VCEStartDt" runat="server" TargetControlID="CVStartDt"
HighlightCssClass="valCalloutint" /> // Checks that the CV is working correctly
<asp:RequiredFieldValidator ID="RFVStartDt" runat="server" ControlToValidate="txtStartDt"
Display="None" ErrorMessage="Start Date is required" ValidationGroup="ReportDate" /> // ensures a value was added to the textbox
<ajax:ValidatorCalloutExtender ID="VCEreqStartDt" runat="server" TargetControlID="RFVStartDt"
HighlightCssClass="valCalloutint" PopupPosition="BottomRight" /> // Checks that the RFV is working correctly
// End Date
<asp:TextBox ID="txtEndDt" runat="server" CssClass="textboxint" />
<ajax:CalendarExtender ID="calEndDt" runat="server" PopupPosition="BottomRight" TargetControlID="txtEndDt" />
<asp:CompareValidator ID="CVEndDt" runat="server" Type="Date" Operator="GreaterThan"
ErrorMessage="End Date must come after the Start Date" ValidationGroup="ReportDate"
ControlToValidate="txtEndDt" ControlToCompare="txtStartDt" Display="None" />
<ajax:ValidatorCalloutExtender ID="VCEEndDt" runat="server" TargetControlID="CVEndDt"
HighlightCssClass="valCalloutint" />
<asp:RequiredFieldValidator ID="RFVEndDt" runat="server" ControlToValidate="txtEndDt"
Display="None" ErrorMessage="End Date is required" ValidationGroup="ReportDate" />
<ajax:ValidatorCalloutExtender ID="VCEreqEndDt" runat="server" TargetControlID="RFVEndDt"
HighlightCssClass="valCalloutint" PopupPosition="BottomRight" />
Note that the extra ValidatorCalloutExtender tools are not redundant, but actually allows the program to prevent the user from entering an empty value for one of the textboxes.
Hope this helps!!
The calendar control should have a property called SelectedDate. You can compare this property with any other DateTime property. The code snippet below will check if the SelectedDate is greater than today.
if (Calendar.SelectedDate > DateTime.Now())
{
}

CompareValidator fails when both numbers are negative in ASP.NET

I am populating two dropdownlists using an SQL datasource with the years of a number of events. I want to make sure that the second year is greater than or equal to the first. However, some of the dates are negative, indicating they are BCE. My CompareValidator works when the second year is positive but if both are negative it tells me that the second year is less than the first.
For example:
Year1: -2000
Year2: 123
This is fine and there is no problem with the comparison.
However if
Year1: -227
Year2: -13
It sometimes says Year2 is less than Year1.
Here is the code:
Start Date:
<asp:DropDownList ID="Year1DropDown" runat="server"
DataSourceID="YearDataSource" DataValueField="YEAR" DataTextField="YEAR"
OnDataBound="AddDefaultItemYear1">
</asp:DropDownList><br />
End Date:
<asp:DropDownList ID="Year2DropDown" runat="server"
DataSourceID="YearDataSource" DataValueField="YEAR" DataTextField="YEAR"
OnDataBound="AddDefaultItemYear2">
</asp:DropDownList><br />
<asp:CompareValidator ID="CompareYears" runat="server"
ControlToValidate="Year2DropDown" ControlToCompare="Year1DropDown"
Operator="GreaterThanEqual" ErrorMessage="End date must be greater than or equal to start date"></asp:CompareValidator>
You must spesify the Type to Integer, because the default data type is String.
<asp:CompareValidator ID="CompareYears" runat="server"
Type="Integer"
ControlToValidate="Year2DropDown" ControlToCompare="Year1DropDown"
Operator="GreaterThanEqual" ErrorMessage="End date must be greater than or equal to start date"></asp:CompareValidator>

cheching date consisting from 3 drop boxes Using CompareValidator

I have 3 drop boxes which correspond to the Day, Month and A Year. After a user chooses a date I concatenate 3 strings and pass it to database.
My problem is how can I validate the date interactively before I pass it to database. To filter out invalid days like June 31 or Feb 30 which do not exist?
I thought about Comparevalidator. what I will do is I create 2 hidden textboxes, populate one with original user input string, and another one with user input string converted to date(if the date is not valid system should convert it to the nearest possible I think).
Than I compare those textBoxes using CompareValidator if they do not match error will be prompt. However somehow it doesn't work. Does anybody have an I idea why. or maybe someone knows less elaborate technique to check the date which is coming from drop boxes?
Will be grateful for any advice or suggestion.
Front end code:
<asp:DropDownList ID="Dates" runat="server" >
<asp:ListItem></asp:ListItem>
<asp:ListItem>01</asp:ListItem>
<asp:ListItem>02</asp:ListItem>
<asp:ListItem>03</asp:ListItem>
.......
<asp:ListItem>29</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>31</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Monthes" runat="server" >
<asp:ListItem></asp:ListItem>
<asp:ListItem>January</asp:ListItem>
.......
<asp:ListItem>December</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="years" runat="server">
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="UserInput" runat="server"></asp:TextBox>
<asp:TextBox ID="ConvertedInput" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" class="errorMess" runat="server"
ControlToCompare="UserInput" ControlToValidate="ConvertedInput"
ErrorMessage="Invalid Date"></asp:CompareValidator>
Back end code:
If Not Page.IsPostBack Then
For count As Integer = 0 To 60
years.Items.Add(CurYear - count)
Next
End If
ConvertedInput.Visible = False
UserInput.Visible = False
.....
Bday = years.Text & "-" & Monthes.Text & "-" & Dates.Text
Dim TempDate As Date = CDate(Bday)
Dim ConvDate As String = TempDate.ToString("yyyy-MM-dd")
UserInput.Text = Bday
ConvertedInput.Text = ConvDate
you could use a customvalidator, build the date and verify it's a valid date. However I would recommend going with a straight textbox and using a UI library like jquery-ui to convert the textbox into a datepicker. you can then pull use DateTime.TryParse() to pull the date out of the textbox in the code behind.
the biggest advantage is only needing to deal with a single input and no string concatination/formatting.

validate a textbox value for the current date in asp.net

i have a calendar control whose value will be displayed in a textbox, i need to validate the textbox value to the current date... It should not be less than current date.....
Thanks for ur valuable reply for my last post
You can use the CompareValidator control, like this:
<asp:CompareValidator ID="dateValidator"
ControlToValidate="IdOfTextBox"
Text="error message"
Operator="GreaterThanEqual"
Type="Date"
runat="server" />
And in the code-behind, set the ValueToCompare property of the validator to DateTime.Today (for example in the Page_Init method).
Try This in Code Behind :
<asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="Date Should be Greater Than Current Date"
ControlToValidate="txt_DateFrom" ValuetoCompare='<%# DateTime.Now.ToString("d") %>' SetFocusOnError="true" Display="Dynamic" />
Use CompareValidator control, set properties as below:
operator = GreaterThanEqual
Type = Date
ValueToCompare = CurrentDate

Resources