验证日期文本框 [英] Validate the Date In Textbox

查看:119
本文介绍了验证日期文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能确定在我的文本的值是一个正确的日期

例如:
这是在我的文本框的日期值: 01/32/2013

我知道这是日期的错误值,但我怎么能确定它是一个错误的格式?
我能避免通过使用 try和catch ,但我不希望使用它,我会使用它,如果我没有找到在我的问题的解决方案。

 < ASP:文本框ID =txtMEditStartDt=服务器文本='<%#绑定(StartDt,{0:MM / DD / YYYY} )%>'
    的CssClass =日期选择SkinID =textSkin宽度=100像素>&下; / ASP:文本框>
< ASP:MaskedEditExtender ID =MaskedEditStartDt=服务器
    的TargetControlID =txtMEditStartDt面具=99/99/9999MaskType =日期
    CultureAMPMPlaceholder =CultureCurrencySymbolPlaceholder =
    CultureDateFormat =CultureDatePlaceholder =CultureDecimalPlaceholder =
    CultureThousandsPlaceholder =CultureTimePlaceholder =启用=真/>
< ASP:CalendarExtender ID =calendarEditStartDt=服务器的TargetControlID =txtMEditStartDt格式=MM / DD / YYYY>
< / ASP:CalendarExtender>


解决方案

您可以使用的 DateTime.TryParse 方法。在使用该方法是一种的电流的应用程序了文化。因此,在失败的情况下,它会简单的返回

 的DateTime DATEVALUE =默认值(日期时间);
如果(DateTime.TryParse(dateString,出DATEVALUE))
   //成功
其他
  //失败

How can I determine if the value in my textbox is a correct date?

ex: this is the value of the date in my Textbox:01/32/2013

I know that it is a wrong value of date, but how can I determine that it is a wrong format? I can avoid that by using try and catch, but I don't want to use it, I will use it if I don't find a solution in my problem.

<asp:TextBox ID="txtMEditStartDt" runat="server" Text='<%# Bind("StartDt", "{0:MM/dd/yyyy}") %>'
    CssClass="datePicker" SkinID="textSkin" Width="100px"></asp:TextBox>
<asp:MaskedEditExtender ID="MaskedEditStartDt" runat="server"
    TargetControlID="txtMEditStartDt" Mask="99/99/9999" MaskType="Date" 
    CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder="" 
    CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder="" 
    CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True"/>
<asp:CalendarExtender ID="calendarEditStartDt" runat="server" TargetControlID="txtMEditStartDt" Format="MM/dd/yyyy">
</asp:CalendarExtender>

解决方案

You can use DateTime.TryParse method. In this method used is a current culture of applicaiton. So in case of failure, it will return simply false.

DateTime dateValue = default(DateTime);
if (DateTime.TryParse(dateString, out dateValue)) 
   //SUCCESS 
else 
  //FAILURE

这篇关于验证日期文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆