ASP.NET 验证器比较两个日期相差不超过 12 个月

发布于 2024-08-22 05:55:31 字数 102 浏览 7 评论 0原文

我有两个用于开始日期和日期的 TextBox 控件。输入结束日期。我必须验证结束日期不大于开始日期&开始日期和开始日期之间的差异结束日期不超过 12 个月。

I have two TextBox controls for start date & end date input. I have to validate that end date is not greater than start date & the difference between start date & end date is not more than 12 months.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

惯饮孤独 2024-08-29 05:55:31

您必须使用 CustomValidator< /code>来执行此操作。在您的 markyou 中,您将具有如下内容:

<asp:TextBox ID="txbStartDate" runat="server" />
<asp:TextBox ID="txbEndDate" runat="server" />
<asp:CustomValidator OnServerValidate="ValidateDuration"
    ErrorMessage="Dates are too far apart" runat="server" />

在后面的代码中,您定义验证处理程序:

protected void ValidateDuration(object sender, ServerValidateEventArgs e)
{
    DateTime start = DateTime.Parse(txbStartDate.Text);
    DateTime end = DateTime.Parse(txbEndDate.Text);

    int months = (end.Month - start.Month) + 12 * (end.Year - start.Year);

    e.IsValid = months < 12.0;
}

请注意,上面的代码容易引发异常。您需要添加其他验证器来检查输入的日期是否可以被解析,并且应该修改 ValidateDuration 方法以确认这些其他验证器在进行自己的测试之前已通过。

此外,您可能还想添加另一个验证器来测试结束日期实际上是否大于(或等于)开始日期。违反此规则可能会引发其自己的验证错误消息。

<asp:CompareValidator Operator="GreaterThanEqual" Type="Date"
    ControlToValidate="txbEndDate" ControlToCompare="txbStartDate"
    ErrorMessage="Let's get started first!" runat="server" />

You will have to use a CustomValidator to do this. In your markyou, you will have something like this:

<asp:TextBox ID="txbStartDate" runat="server" />
<asp:TextBox ID="txbEndDate" runat="server" />
<asp:CustomValidator OnServerValidate="ValidateDuration"
    ErrorMessage="Dates are too far apart" runat="server" />

And in your code behind, you define the validation handler:

protected void ValidateDuration(object sender, ServerValidateEventArgs e)
{
    DateTime start = DateTime.Parse(txbStartDate.Text);
    DateTime end = DateTime.Parse(txbEndDate.Text);

    int months = (end.Month - start.Month) + 12 * (end.Year - start.Year);

    e.IsValid = months < 12.0;
}

Note that the code above is prone to throw exceptions. You will need to add additional validators to check that the dates entered can be parsed, and the ValidateDuration method should be modified to confirm that these other validators have passed before doing its own tests.

Further, you might want to yet add another validator to test that the end date is in fact greater (or equal to) the start date. Breaking this rule should probably raise its own validation error message.

<asp:CompareValidator Operator="GreaterThanEqual" Type="Date"
    ControlToValidate="txbEndDate" ControlToCompare="txbStartDate"
    ErrorMessage="Let's get started first!" runat="server" />
面犯桃花 2024-08-29 05:55:31

您也可以使用时间跨度:

        DateTime start = DateTime.Parse(DateBegin.Text);
        DateTime end = DateTime.Parse(DateEnd.Text);
        TimeSpan ts = end - start;
        e.IsValid = ts.Days < 365;

Also you can use Timespan:

        DateTime start = DateTime.Parse(DateBegin.Text);
        DateTime end = DateTime.Parse(DateEnd.Text);
        TimeSpan ts = end - start;
        e.IsValid = ts.Days < 365;
凹づ凸ル 2024-08-29 05:55:31

快速而简单:两个验证器,一个是比较验证器(比较两个控件),另一个是带有服务器端方法来检查结束日期的自定义验证器。

Quick and easy: Two validators, one a Comparison Validator (which compares both controls), and a Custom Validator with a server-side method to check the end date.

橘虞初梦 2024-08-29 05:55:31

为什么你不关心这个

 DateTime start = DateTime.Parse(DateBegin.Text);
 DateTime end = DateTime.Parse(DateEnd.Text);
 e.IsValid = (end-start).Years <1;

And why you're not about that

 DateTime start = DateTime.Parse(DateBegin.Text);
 DateTime end = DateTime.Parse(DateEnd.Text);
 e.IsValid = (end-start).Years <1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文