日期验证 [英] date validation

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

问题描述

任何一个可以告诉我,code,以验证在注册表格的日期字段(SHD也验证天的闰年,没有每个月

can any one tell me the code to validate the date field in signup form(which shd also validate the leap year and no of days in each month

推荐答案

这取决于输入,但假设你有整个日期字符串输入,那么你可以尝试这样的:

That depends on the input but suppose you have a string input for the whole date then you could try something like:

try
{
     DateTime parsedDate = DateTime.Parse(incomingDateString);
}
catch
{
     throw new Exception("Incoming Date string could not be parsed as a DateTime");
}

另外,如果你有三个整数从表单来的,然后你将与

Alternatively if you have three integers as strings coming in from the form then you would replace the DateTime.Parse with

     DateTime parsedDate = new DateTime(Int32.Parse(yearString), Int32.Parse(MonthString), Int32.Parse(DayString));

和允许DateTime构造带的分析当月天的细节关怀和闰年。你可能会更加复杂和使用Int32.TryParse以及如果多数民众赞成你所需要的空字符串提供更具体的错误信息和检查。

and allow the DateTime constructor take care of the details of analyzing days of month and leap years. You could be more sophisticated and use Int32.TryParse and provide more specific error messages and checks for null strings if thats what you need.

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

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