Solved

asp.net, c#

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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