?
Solved

asp.net, c#

Posted on 2014-02-19
3
Medium Priority
?
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Accepted Solution

by:
CtrlAltDl earned 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This article discusses how to implement server side field validation and display customized error messages to the client.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

743 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