简体   繁体   English

如何检查或验证文本框输入日期是DD / MM / YYYY格式?

[英]How to check or validate the textbox entered date is in DD/MM/YYYY format?

如何检查或验证文本框输入日期是DD/MM/YYYY格式?

Markup: 标记:

<asp:Textbox runat="server" ID="TextBox1" />
<asp:CustomValidator runat="server" ControlToValidate="TextBox1" ErrorMessage="Date was in incorrect format" OnServerValidate="CustomValidator1_ServerValidate" />

Code-behind: 代码隐藏:

protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
{
    DateTime d;
    e.IsValid = DateTime.TryParseExact(e.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);
}

if you want to allow several formats and only them, use next: 如果你想允许几种格式而只允许它们,请使用下一个:

DateTime.TryParseExact(e.Value, new[] { "dd/MM/yyyy", "yyyy-MM-dd" }, CultureInfo.InvarinatCulture, DateTimeStyles.None, out d);

Another option is using a regular expression validator. 另一种选择是使用正则表达式验证器。 The regular expression below checks for DD/MM/YYYY but of course there is no way to distinguish if something like 01 is DD or MM. 下面的正则表达式检查DD / MM / YYYY但当然没有办法区分01之类的东西是DD还是MM。 Otherwise it does the trick. 否则就可以了。

<asp:TextBox ID="txtDate" runat="server"/>
<asp:RegularExpressionValidator ID="regexpName" runat="server"     
                                ErrorMessage="This expression does not validate." 
                                ControlToValidate="txtDate"     
                                ValidationExpression="^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$" />
DateTime Result;
DateTimeFormatInfo info = new DateTimeFormatInfo ( );
CultureInfo culture;
culture = CultureInfo.CreateSpecificCulture ( "en-US" );
info.ShortDatePattern = "dd/MM/yyyy";
if ( DateTime.TryParse ( StrDate, info, DateTimeStyles.None, out Result ) )
{
  return StrDate;
}

You could use a CustomValidator to check that the entered value is within set parameters. 您可以使用CustomValidator检查输入的值是否在设置参数范围内。
Ie 13/12/2001 is valid but 12/13/2001 is invalid. 即13/12/2001有效但12/13/2001无效。

You can use a Custom Validator to check just about anything in text box, but it might be easier to use CompareValidator to check if the text can be converted to a date. 您可以使用自定义验证器检查文本框中的任何内容,但使用CompareValidator检查文本是否可以转换为日期可能更容易。

I found an example (in VB but easy enough to read and translate to C#) here: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/validation/default.aspx 我在这里找到了一个例子(在VB中,但很容易阅读和翻译成C#): http//quickstarts.asp.net/QuickStartv20/aspnet/doc/validation/default.aspx

However, using some sort of date picker control might be more user friendly. 但是,使用某种日期选择器控件可能更加用户友好。

You will be better off using a calendar control, but if you want to check the date string anyway, use the following code can be used to validate that the is in DD/MM/YYYY format. 您最好使用日历控件,但如果您想检查日期字符串,请使用以下代码验证是否为DD / MM / YYYY格式。

DateTime dt;
if (!DateTime.TryParse(texbox.Text, new  System.Globalization.CultureInfo("en-GB"), System.Globalization.DateTimeStyles.None, dt))
{
    // text is not in the correct format
}

My approach would be to use a regexvalidator for a first quick test ("does it look like it could be a date") client-side. 我的方法是使用regexvalidator进行第一次快速测试(“看起来它可能是一个日期”)客户端。 And then a CustomValidator with a serverside event that tries to perform a DateTime.Parse with my format, to rule out dates like "29 feb 2010". 然后一个带有服务器端事件的CustomValidator尝试使用我的格式执行DateTime.Parse,以排除像“29 feb 2010”这样的日期。

Personally I don't trust the CompareValidator here, because I don't know for sure what format it uses (but that can be because I didn't investigate enough). 我个人不相信CompareValidator,因为我不确定它使用的是什么格式(但这可能是因为我没有进行足够的调查)。

You can't. 你不能。 User some kind of datetime picker instead. 用户可以使用某种日期时间选择器。 Example here . 这里的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM