Solved

asp.net, c#

Posted on 2014-02-19
3
178 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

724 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