验证代表出生日期的3个文本字段 [英] validate 3 textfields representing date of birth

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

问题描述

我有3个文本框字段.代表一个日期

I have 3 text box fields. to represent a date

例如DD MM YYYY

eg DD MM YYYY

我如何才能验证仅正确的数据输入到每个文本框中.它是正则表达式吗?

how can i validate only correct data is entered into each text box. is it a regexpression??

我需要在ascx/aspx文件中而不是在后面的.cs代码中完成此操作

谢谢

推荐答案

您可以使用正则表达式验证每个字段,但不会考虑不同天数的不同月份:您可以输入无效的日期.

You could validate each field with regexes, but it wouldn't take into account different months with different numbers of days: you could enter invalid dates.

在服务器端,可以使用类似以下内容的方法进行验证:

On the server side it could be validated with something like this:

DateTime D;
string CombinedDate=String.Format("{0}-{1}-{2}", YearField.Text, MonthField.Text, DayField.Text);
if(DateTime.TryParseExact(CombinedDate, "yyyy-M-d", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out D)) {
  // valid
} else {
  // not valid
}

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

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