Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

correct date and time in textbox

Posted on 2005-03-29
8
Medium Priority
?
193 Views
Last Modified: 2010-04-07
Urgent

Hi

Whats the best way to do the following?

ensure that the user always enters the date and time correctly into a textbox.

dd/mm/yy hh:mm
25/02/05 14:23

validation
textbox mask
or something else

im thinking textbox mask is the best but dont know how to do that in asp.net

can anyone help?
0
Comment
Question by:narmi2
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 11

Expert Comment

by:sachiek
ID: 13650858
The simplest approach would just be to create a method that returns a bool and tries to parse the text into a DateTime object. The DateTime.Parse(string) method should work just fine, and if the text is not in the correct format you will catch the exception thrown. Once you have the exception, you can throw error

I would also either go with the validation events, or the TextChanged event to try and parse out the text into a DateTime object.

0
 
LVL 11

Expert Comment

by:sachiek
ID: 13650881
You can modify culture according to your need.

System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-GB");
DateTime a = DateTime.Parse(txt1.text,culture,System.Globalization.DateTimeStyles.NoCurrentDateDefault);

Now use try catch and validate it.

0
 
LVL 1

Author Comment

by:narmi2
ID: 13650941
I was hoping for something more client side, which does not have to do a round trip to the server.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Expert Comment

by:brdrok
ID: 13651778
have you considered using a pop up calendar?

btnCalendar.Attributes["onClick"]=@"javascript:calendar_window=window.open('Calendar.aspx?formname=frmAddOrders.txtDueOn', 'calendar_window', 'width=200, height=200'); calendar_window.focus(); return true;";

assuming that you have a web form called calendar.aspx

0
 
LVL 7

Expert Comment

by:brdrok
ID: 13651780
and a button called btnCalendar

0
 
LVL 7

Expert Comment

by:brdrok
ID: 13651782
my fault...didn't see that you needed the time as well...in that case a calendar probably wouldn't do much good.
0
 
LVL 7

Accepted Solution

by:
ayha1999 earned 2000 total points
ID: 13652709
Hi,

use this regularexpression for RegularExpression validaotr.

(?n:^(?=\d)((?<day>31(?!(.0?[2469]|11))|30(?!.0?2)|29(?(.0?2
)(?=.{3,4}(1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(1
6|[2468][048]|[3579][26])00))|0?[1-9]|1\d|2[0-8])(?<sep>[/.-
])(?<month>0?[1-9]|1[012])\2(?<year>(1[6-9]|[2-9]\d)\d{2})(?
:(?=\x20\d)\x20|$))?(?<time>((0?[1-9]|1[012])(:[0-5]\d){0,2}
(?i:\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$)

hope this helps.

ayha
0
 
LVL 7

Assisted Solution

by:ayha1999
ayha1999 earned 2000 total points
ID: 13652856
Hi again,

pls. visit the following site where u can;t find regularexpression meeting ur requirements.

http://www.regexlib.com/Default.aspx

ayha
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses

580 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