Solved

asp.net, c#

Posted on 2014-02-19
3
174 Views
Last Modified: 2014-04-20
I have two textboxes using for dates. like begin date and end date.
How can I validate begin date must be earlier than end date in code behind (c#)?
0
Comment
Question by:CoolDev2014
3 Comments
 
LVL 14

Accepted Solution

by:
CtrlAltDl earned 500 total points
ID: 39871963
//TextBox1 = Start Date  |  TextBox2 = End Date
if (Convert.ToDateTime(TextBox1.Text)  < Convert.ToDateTime(TextBox2.Text)) {
    //Start Date is less than End Date
} else {
    //End Date is less than Start Date, so send error
}
0
 

Author Comment

by:CoolDev2014
ID: 39872218
i need to do that with the build int validation controller.
0
 
LVL 6

Expert Comment

by:Brijesh Gandhi
ID: 40011292
<asp:CompareValidator id="cvtxtStartDate" runat="server" ControlToCompare="txtStartDate" cultureinvariantvalues="true" display="Dynamic" enableclientscript="true"  ControlToValidate="txtFinishDate" ErrorMessage="Start date must be earlier than finish date" type="Date" setfocusonerror="true" Operator="GreaterThanEqual" text="Start date must be earlier than finish date"></asp:CompareValidator>

Open in new window

But I would suggest to make validation from client side using javascript code as per below
if (Date.parse(fromDate) > Date.parse(toDate)) {
alert("Invalid Date Range!\nStart Date cannot be after End Date!")
return false;
}

Open in new window

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question