Asp.Net End date greater than start date - asp.net

Work on Asp.Net vs 08.
Below is my code
<asp:TextBox ID="txtSTART_DATE" runat="server"></asp:TextBox>
<cc1:PopupControlExtender ID="txtSTART_DATE_PopupControlExtender" runat="server" CommitProperty="value"
CommitScript="e.value += '';" PopupControlID="pnlSTART_DATE"
Position="Bottom" TargetControlID="txtSTART_DATE">
</cc1:PopupControlExtender>
<asp:TextBox ID="txtEND_DATE" runat="server"
ontextchanged="txtEND_DATE_TextChanged"></asp:TextBox>
<cc1:PopupControlExtender ID="txtEND_DATE_PopupControlExtender" runat="server" CommitProperty="value"
CommitScript="e.value += '';" PopupControlID="pnlEND_DATE"
Position="Bottom" TargetControlID="txtEND_DATE">
</cc1:PopupControlExtender>
C# syntax is
protected void dtpSTART_DATE_SelectionChanged(object sender, EventArgs e)
{
AjaxControlToolkit.PopupControlExtender.GetProxyForCurrentPopup(this.Page).Commit(dtpSTART_DATE.SelectedDate.ToString("dd MMM yyyy"));//Set the value
}
protected void dtpEND_DATE_SelectionChanged(object sender, EventArgs e)
{
AjaxControlToolkit.PopupControlExtender.GetProxyForCurrentPopup(this.Page).Commit(dtpEND_DATE.SelectedDate.ToString("dd MMM yyyy"));//Set the value
}
want to compare start date with the end date.Verify that end date greater than start date and today date.How to check the value?

You could use the asp.net CompareValidator, e.g.
<asp:TextBox ID="txtSTART_DATE" runat="server" />
<asp:TextBox ID="txtEND_DATE" runat="server" />
<asp:CompareValidator ID="cmpDates" ControlToValidate="txtEND_DATE"
ControlToCompare="txtSTART_DATE" Operator="GreaterThan" Display="dynamic"
ErrorMessage="End date must be after start date" runat="server" />
This won't do a popup though, but if you are just after a validator then that should be ok.
The compare validator is actually very powerful, for instance, you can check the datatype of the argument as well, so I would use something like this:
<asp:TextBox ID="txtSTART_DATE" runat="server" />
<asp:CompareValidator ID="chkStartIsDate" runat="server" Display="Dynamic"
Operator="DataTypeCheck" Type="Date" ControlToValidate="txtSTART_DATE"
ErrorMessage="You must supply a valid start date" />
<asp:TextBox ID="txtEND_DATE" runat="server" />
<asp:CompareValidator ID="chkEndIsDate" runat="server" Display="Dynamic"
Operator="DataTypeCheck" Type="Date" ControlToValidate="txtEND_DATE"
ErrorMessage="You must supply a valid end date" />
<asp:CompareValidator ID="cmpStartAndEndDates" runat="server" Display="Dynamic"
Operator="GreaterThan" ControlToValidate="txtEND_DATE" ControlToCompare="txtSTART_DATE"
ErrorMessage="The end date must be after the start date" />

<asp:TextBox ID="txtStartDate" runat="server" />
<asp:TextBox ID="txtEndDate" runat="server" />
<asp:CompareValidator ID="cvStartEnd" Operator="GreaterThan" Type="Date"
ControlToValidate="txtEndDate" ControlToCompare="txtStartDate"
ErrorMessage="End date must be greater than start date!" runat="server"/>
Make sure to set property Type="Date" otherwise it would do a string compare and give u wrong results.

Related

How to make a extender calendar unable to select a date base on a selected date?

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" />

ASP.Net Prevent compare validator and regular expression validator from firing at the same time

I've got two text boxes like this:
<asp:TextBox ID="textBox1" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Enter a number." Display="Dynamic" />
<asp:TextBox ID="textBox2" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Enter a number." Display="Dynamic" />
<asp:CompareValidator id="compareValidator" runat="server" ControlToValidate="textBox2" ControlToCompare="textBox1" Type="Double" Display="Dynamic" Operator="LessThan" Text="Error." />
It's two text boxes that have regular expression validators that constrain them to only allow positive real numbers. These work fine.
I also want the input in the second text box to be smaller than the first one. For that I have a compare validator.
When the user has correct numbers the compare validator works fine.
It's when they enter in anything that fails the second regular expression validator, that the compare validator also fires at the same time.
It doesn't matter what's in the first text box, valid input, wrong input, or even nothing. Both of the second validators fail.
Even though the validator is supposed to be comparing doubles.
Is there an easy fix for this?
I realize that this behavior is okay, because a validator is invalid when it should be, but the user would be seeing the incorrect error messages.
I've already done a solution involving custom validators and Javascript, and if it comes down to it then I'll have to do that again. But if that's the case then there's not much point to using a compare validator, since it'll never work with a regular expression validator.
Assign different validation groups to regular expression validator and compare validator and use ASP.NET inbuilt javascript function Page_ClientValidate() to check validation one by one.
<asp:TextBox ID="textBox1" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="reqFldTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Enter a number." Display="Dynamic" ValidationGroup="Group2"/>
<asp:TextBox ID="textBox2" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="reqFldTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Enter a number." Display="Dynamic" ValidationGroup="Group2" />
<asp:CompareValidator id="compareValidator" runat="server" ControlToValidate="textBox2" ControlToCompare="textBox1" Type="Double" Display="Dynamic" Operator="LessThan" Text="Error." ValidationGroup="Group3" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate()" />
In Javascript
<script type="text/javascript">
function Validate() {
var isValid = false;
isValid = Page_ClientValidate('Group1');
if (isValid) {
isValid = Page_ClientValidate('Group2');
}
if (isValid) {
isValid = Page_ClientValidate('Group3');
}
return isValid;
}
</script>
I think you should use Custom Validator for second textbox to confirm that Comparison logic occurs if regular expression validation passes. You could do it the following way-
<asp:Textbox id="textBox2" runat="server" text=""></asp:Textbox>
<asp:CustomValidator id="CustomValidator2" runat="server"
ControlToValidate = "textBox2"
ErrorMessage = "Your Error Message"
ClientValidationFunction="validateLength" >
</asp:CustomValidator>
<script type="text/javascript">
function validateLength(oSrc, args){
// your validation logic here
}
</script>

Validate end date should be greater than start date?

datePicker.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" />
register.aspx
<%# Register Src="~/Modules/datePicker.ascx" TagName ="datePicker" TagPrefix ="uc1" %>
Start Date <uc1:datePicker ID="datePickerSD" runat="server" >
End Date <uc1:datePicker ID="datePickerED" runat="server" />
<asp:CustomValidator ID="CustomValidator1" ControlToValidate="datePickerED"
ErrorMessage="End date should be greater than start date" runat="server" />
Tried to get clientid of start date..but confused..
<asp:CompareValidator ID="cmpr" runat="server" ControlToCompare ="txtDate1" ControlToValidate="txtDate2" Operator="GreaterThan" Type="Date" ErrorMessage="To date should great than small data"></asp:CompareValidator>
Have you tried compare validator ? This can help you
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>

user control (.ascx) with a textbox and a ajax calendar extender - end date should be greater than start date

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>

CompareValidator asp.net Type:String

I am trying to use CompareValidator witch compare the field for string.and the
server code to write in the Label1 the result.I tried with integer and date data types and it
work fine but when i put string its not working.When i put integer in the field it consider that is true.How to make it work.
<asp:TextBox ID="name" runat="server" CausesValidation="True" MaxLength="40"> </asp:TextBox>
<asp:CompareValidator
ID="CompareValidator1"
runat="server"
ControlToValidate="name"
Operator="DataTypeCheck"
Type="String">
</asp:CompareValidator>
<asp:Label ID="Label1" runat="server" Text="Label" Visible="True"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Изпрати" BackColor="Black" BorderColor="Black" BorderStyle="None" ForeColor="White" OnClick="PageValidate_SendMail" />
protected void PageValidate_SendMail(object sender, EventArgs e)
{
if (Page.IsValid) {
Label1.Text = "String";
}
else {
Label1.Text = "Integer";
}
}
What you will need is RegularExpressionValidator something like below. The ValidationExpression="^[a-zA-Z]*$ restricts input to alphabets only.
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="name" ValidationExpression="^[a-zA-Z]*$" ErrorMessage="RegularExpressionValidator" Display="Dynamic"></asp:RegularExpressionValidator>
You might want to use RequiredFieldValidator as well for checking empty input.
And if none of those meet your requirements then you will need to use CustomValidator.

Resources