简体   繁体   English

如何检查两个日期的两个文本框

[英]How do I check two textboxes for two dates

One has txtDateReceived and second has txtVendorPackDate. 一个具有txtDateReceived,第二个具有txtVendorPackDate。 Before insert will add record I have to check if txtDateReceived is not erlier then txtVendorPackDate. 在插入将添加记录之前,我必须检查txtDateReceived是否正确,然后检查txtVendorPackDate。 I try use TextChanged event. 我尝试使用TextChanged事件。

protected void txtVendorPackDate_TextChanged(object sender, EventArgs e)
{
    DateTime fromDate = DateTime.MinValue;
    DateTime toDate = DateTime.MaxValue;
    bool parseResultMin = DateTime.TryParse(txtVendorPackDate.Text, out fromDate);
    bool parseResultMax = DateTime.TryParse(txtDateReceived.Text, out toDate);
    if (toDate < fromDate)
    {
        txtVendorPackDate.Text = "";
        lblDateExpired.Visible = true;
        lblDateExpired.Text = "Selected date is incorrect, please enter correct data.";
        txtVendorFatPerc.Focus();
    }

    double expired = toDate.Subtract(fromDate).TotalDays;

    if (expired >= 60)
    {

        lblDateExpired.Text = "Date Expired " + expired + " days after pack day!!!" 
        lblDateExpired.Visible = true;
    }  
} 

How I could do it from client side not using controls validation. 我如何从客户端不使用控件验证来做到这一点。

Try this 尝试这个

if (!parseResultMin || !parseResultMax || toDate < fromDate)

In your code, if both dates are invalid, toDate and fromDate will both be DateTime.MinValue , so the expression toDate < fromDate won't be true. 在您的代码中,如果两个日期均无效,则toDatefromDate都将为DateTime.MinValue ,因此表达式toDate < fromDate不会为true。

You can use a CompareValidator control to check that the vendor pack date is less than the received date. 您可以使用CompareValidator控件来检查供应商包装日期是否少于收到的日期。 If both fields are required, you can also use the RequiredFieldValidator. 如果两个字段都是必需的,则还可以使用RequiredFieldValidator。 I would employ a combination of RequiredFieldValidators and CompareValidators. 我将使用RequiredFieldValidators和CompareValidators的组合。

One RequiredFieldValidator for each textbox to make sure the user enters a value. 每个文本框都有一个RequiredFieldValidator,以确保用户输入一个值。 One CompareValidator for each textbox to make sure the value entered is a date type. 每个文本框都有一个CompareValidator,以确保输入的值是日期类型。 One CompareValidator to make sure the vendor pack date is earlier than the received date. 一个CompareValidator来确保供应商装箱日期早于收到日期。

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

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