Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Firstly, all the validators don't fire exactly at same time. They seem so as it happens in a fraction of seconds.</p> <p>The validators that you add in a <code>.aspx</code> page, they are added to <code>Page.Validators</code> collection in the same order they are created/added to page. The validation runs in the order they are present in the <code>Page.Validators</code> collection.Thus the first validator in the aspx file is first in Page.Validators. If you want to rearrange the order, then the correct way is to arrange your validators in the page in the same order you want them to fire.</p> <p><strong>NOTE</strong>: The validators will fire one by one. in case you don't want the very next validators to fire you may use Javascript to disable the next ones. call a ClientValidation function in first validator</p> <pre><code>&lt;asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox3" ClientValidationFunction="disableNextVal" .... /&gt; </code></pre> <p>// Sample JavaScript code</p> <pre><code>function disableNextVal() { // firstly check here for first condition, if First condition fails, // disable the next validator as below. var nextVal = document.getElementById('nextValidatorClientID'); ValidatorEnable(myVal, false); // or use this one: myVal.enabled = false; } </code></pre> <p>However, one more solution and possibly the better one is mentioned below.</p> <p><em>In these scenarios where the value entered in TextBox should pass multiple conditions like:data format, Value should be greater than some minimum required value etc.. it is always good to use a <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx" rel="nofollow"><strong><code>CustomValidator</code></strong></a> control.</em></p> <p>In this custom validator control check one by one each of your conditions. If first condition fails: date is not valid, do not check others and display the error message for first one only. Similarly, if second condition fails: range is not valid, display message for second failed condition only. </p> <pre><code>&lt;asp:CustomValidator ID= "valxTextBox" runat="server"Enabled="true" ControlToValidate="txtDate" ClientValidationFunction ="ValidateTxtDate" ValidateEmptyText="true" OnServerValidate="valxTextBox_ValidatePostalCode" &gt;&lt;/asp:CustomValidator&gt; </code></pre> <p>As you see, this gives the flexibility to define your custom Client Side as well as server side events for Validation.</p> <p>In your server validation, check for conditions one by one, and return as soon as you find one failing.</p> <p><strong>For validating data against regularExpressions, use <a href="http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx" rel="nofollow"></strong><code>Regex</code><strong></a> class of <code>System.Text.RegularExpressions</code> namespace.</strong></p> <pre><code>protected void valeEmailAddress_txtEmailAddressValidate(object sender, ServerValidateEventArgs e) { string MaximumValue="09/25/2013"; string MinimumValue="1/1/2012"; // check for first condition if(txtTravelerDOB &gt;MaximumValue ||txtTravelerDOB &lt; MinimumValue ) { // sample code here // if failing, set IsValid to false e.IsValid = false; // dynamically set the error message as per first condition valxTextBox.ErrorMessage ="Not a valid date"; } // check for second condition // read the expression pattern from appSettings if(!Regex.Match(txtTravelerDOB.Text.Trim(), WebConfigurationManager.AppSettings("travelerDOBRegEX")).Success) { // if fails, e.IsValid = false; valxTextBox.ErrorMessage ="Format is Invalid"; } } </code></pre> <p>Appsettings Value:</p> <pre><code>&lt;add key="travelerDOBRegEX" value="^(((0?[13578]|1[02])[\/](0?[1-9]|[12]\d|3[01])[\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[13456789]|1[012])[\/](0?[1-9]|[12]\d|30)[\/]((1[6-9]|[2-9]\d)?\d{2}))|(0?2[\/](0?[1-9]|1\d|2[0-8])[\/]((1[6-9]|[2-9]\d)?\d{2}))|(0?2[\/]29[\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))$"/&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