Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You will have to use a <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx" rel="noreferrer"><code>CustomValidator</code></a> to do this. In your markyou, you will have something like this:</p> <pre><code>&lt;asp:TextBox ID="txbStartDate" runat="server" /&gt; &lt;asp:TextBox ID="txbEndDate" runat="server" /&gt; &lt;asp:CustomValidator OnServerValidate="ValidateDuration" ErrorMessage="Dates are too far apart" runat="server" /&gt; </code></pre> <p>And in your code behind, you define the validation handler:</p> <pre><code>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 &lt; 12.0; } </code></pre> <p>Note that the code above is <strong>prone to throw exceptions</strong>. You will need to add additional validators to check that the dates entered can be parsed, and the <code>ValidateDuration</code> method should be modified to confirm that these other validators have passed before doing its own tests.</p> <p>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.</p> <pre><code>&lt;asp:CompareValidator Operator="GreaterThanEqual" Type="Date" ControlToValidate="txbEndDate" ControlToCompare="txbStartDate" ErrorMessage="Let's get started first!" runat="server" /&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload