简体   繁体   中英

Compare Validator for two dates

I have two labels and two text boxes, a Compare validator and a button.

I need it to compare two dates (rental date , return date ) and when the rental date is less or equal to return date are the same. No validation message.

While when when the rental date is less than the return date, display an input error message.

The compare validator has been set with :

controltocompare : txtrental,
controltovalidate: txtreturndate,
operator :greater than equal,
type:date,
errormessage: return date must be greater or equal than rental date,

I am not sure how to get the btn to display it ?

您需要将按钮的属性“ CausesValidation ”设置为“ true”,以在单击时触发验证。

确保CompareValidator具有runat="server"

  1. Create a method to display message.

    private void AlertBox(string Msg) { string s = "alert('" + Msg + "')"; ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ckey", s, true); }

  2. find the code to validate and throw alert message.

      if (!String.IsNullOrEmpty(txtrental.Text) && !String.IsNullOrEmpty(txtreturndate.Text)) { DateTime ssSD = Convert.ToDateTime(txtrental.Text); DateTime qsED = Convert.ToDateTime(txtreturndate.Text); int chktxtfd1_sd = ssSD.CompareTo(qsSD); if ((chktxtfd1_sd == 0 || chktxtfd1_sd == -1) ) { //do something bcoz condition is true } else { lvflag = false; AlertBox("date must be greater or equal than rental date"); } } 

If you find it useful, please mark it as your answer else let me know...

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