CompareValidator fails when both numbers are negative in ASP.NET - 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>

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?

Asp.net Range Validator

Html (Full Code)
<asp:TextBox ID="txtSasiNo" runat="server" Width="250px" MaxLength="17"></asp:TextBox>
<asp:RegularExpressionValidator CssClass="zorunlu" ValidationGroup="kaskoTeklifSayfasi" Display="Dynamic" ID="rangevalidator1" runat="server" SetFocusOnError="true" ControlToValidate="txtSasiNo" ErrorMessage="Enter number characters only (8 or 17)" ValidationExpression="^\d{8}$|^\d{17}$""></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="rfvSasiNo" SetFocusOnError="true" ValidationGroup="kaskoTeklifSayfasi" runat="server" Display="Dynamic" ControlToValidate="txtSasiNo" CssClass="zorunlu">Please enter Number.</asp:RequiredFieldValidator>
Question:
I am trying to use RangeValidator.
If txtnumber is null or empty and if txtnumber is not 8 characters or not 17 characters.
Only 2 options first option is 8 character second option is 17 character.
txtnumber text character only can be like below
12345678 // This is 8 characters
12345678901234567 // This is 17 characters
What to add to RangeValidator in order to achieve this ?
Thanks.
As described by Ondrej you can use RegularExpressionValidator. To be more specific with the aspx. See the code below:-
UPDATED CODE:
Here you go:-
<asp:TextBox ID="txtnumber" runat="server" Width="250px"></asp:TextBox>
<asp:RegularExpressionValidator Display="Dynamic" ID="regtxt" runat="server" SetFocusOnError="true" ControlToValidate="txtnumber" ErrorMessage="Enter number characters only (8 or 17)"
ValidationExpression="^\d{8}$|^\d{17}$"></asp:RegularExpressionValidator>
Hope it helps
Don't use range validator - use regex validator instead:
<asp:RegularExpressionValidator ValidationExpression="^([0-9]{8})|([0-9]{17})$" ... />
The validators usually ignore empty fields. Notable exception: the RequiredFieldValidator. Add that to guard against empty fields and keep the RangeValidator to force a specific range of values.

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.

date Compare Validator issue in 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.

Using a CalendarExtender with a MaskedEditExtender

I'm trying to have a textbox function exactly like the third textbox down on this page: http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx. I'm trying to use a CalendarExtender control with a MaskedEditExtender, because I don't want the user to be able to enter anything except a valid date into the box. On my maskededitextender I have a mask of "99/99/9999" but it seems to only work when the date is actually 8 digits (e.g. 12/12/2000) and not when the date is 7 or 6 digits (e.g. 1/1/2000 or 1/14/2000). The mask screws up when the date is less than 8 digits. Here is my code:
<asp:TextBox runat="server" ID="txtDateAvailable" Width="150px" maxlength="50"></asp:TextBox>
<asp:CalendarExtender ID="calDateAvailable" runat="server" TargetControlID="txtDateAvailable" format="d" PopupPosition="Right"></asp:CalendarExtender>
<asp:MaskedEditExtender ID="mskDateAvailable" runat="server" targetcontrolid="txtDateAvailable" Mask="99/99/9999" clearmaskonlostfocus="false" MaskType="None"></asp:MaskedEditExtender>
If I could figure out how to get the date format of the CalendarExtender to MM/DD/YYYY instead of just M/D/YYYY that would fix it.
There is a Format property of Calendar Control. Use that to set to
Format="MM/dd/yyyy"
The syntax for it is as follows: Format="MM/dd/yyyy" and change it around as you wish, but you must ensure that the month is in uppercase otherwise it will return zeros i used -
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1"
PopupButtonID="ImageButton1" Format="dd/MM/yy">

Resources