简体   繁体   中英

How do I check two textboxes for two dates

One has txtDateReceived and second has txtVendorPackDate. Before insert will add record I have to check if txtDateReceived is not erlier then txtVendorPackDate. I try use TextChanged event.

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.

You can use a CompareValidator control to check that the vendor pack date is less than the received date. If both fields are required, you can also use the RequiredFieldValidator. I would employ a combination of RequiredFieldValidators and CompareValidators.

One RequiredFieldValidator for each textbox to make sure the user enters a value. One CompareValidator for each textbox to make sure the value entered is a date type. One CompareValidator to make sure the vendor pack date is earlier than the received date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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