title: Validate user control on parent page
body: <p>How to validate user control textbox through parent page?</p>
  
  <p>User controls are added dynamically on asp.net page and they need to be validated on save button which is on (parent) asp.net page.</p>
  
  <p>Text boxes on user controls are being validated through client script. </p>
  
  <p>Once the controls are added dynamically, validation works and shows error message but values are being saved on 'save' button. If i input '5' then i get error message and if i click the save button the value '5' is saved.</p>
  
  <p><strong>User Control</strong></p>
  
  <pre><code>   &lt;td&gt;
          &lt;asp:TextBox ID="txtEstReportingHrs" runat="server" Width="102px" class="initval"&gt;&lt;/asp:TextBox&gt;
          &lt;asp:CustomValidator ID="rtxtERH" runat="server"  CssClass="required" ValidationGroup="btnSave" SetFocusOnError="True" ControlToValidate="txtEstReportingHrs" ClientValidationFunction="ValidateReportingHrs" Display="Dynamic" ValidateEmptyText="True"&gt;&lt;/asp:CustomValidator&gt;
     &lt;/td&gt;
  </code></pre>
  
  <p><strong>User Control Script</strong></p>
  
  <pre><code>function ValidateReportingHrs(sender, args) {
  var RHMatchexpression = /^(([0-3])?(\.(0|25|5|75)0*)?|4(\.(0|25|5)0*)?)$/;
  var re = new RegExp(RHMatchexpression);
  
  if (args.Value == "") {
      sender.innerHTML = "* Required";
      args.IsValid = false;
      return;
  }
  
  if (args.Value &gt; 4.5) {
      sender.innerHTML = "* excessive reporting time";
      args.IsValid = false;
      return;
  }
  
  
  if (!re.test(args.Value)) {
      sender.innerHTML = "* Invalid";
      args.IsValid = false;
      return;
  }
  
  args.IsValid = true;
  return;
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Asp.net Page</strong></p>
  
  <pre><code>&lt;asp:Button runat="server" Text="Save Details" ID="btnSave" Width="130px" ClientIDMode="Static" ValidationGroup="save"&gt;&lt;/asp:Button&gt;
  </code></pre>