开发者

Validate the string entered is of mm/dd/yyyy format

I have a datepicker control for the users to pick the date, however, they also need to enter the date manually. As such, I need to validate the date entered by the user in the textbox.

Below is the code that I am using to validate

 DateTime Test;
       if ((!string.IsNullOrEmpty(strtdate)))
       {
           bool valid = DateTime.TryParseExact(strtdate, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out Test);
       }

The 开发者_运维知识库date entered by the user is 6/29/2011, however it gives the bool valid value as false though it is correct.

What am I missing here? Please let me know, its urgent.

Thanks.


It's urgent....well you said to let you know. ;-)

I'm assuming TryParseExact fails because there is only one digit for the month and not two as specified in the format field. If you use TryParse instead it should work fine.


use the following format "M/dd/yyyy"
M is for 1..12

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜