title: ASP.NET Custom Validator and AJAX
body: <p>I implemented a Custom Validator with AJAX, and It works fine:</p>
  
  <p><strong>EDITED</strong>. Add ASPX code</p>
  
  <pre><code>&lt;asp:DetailsView ID="DetailsView" runat="server" Height="50px" Width="25em" DataSourceID="SqlDataSource1" AutoGenerateRows="False" DefaultMode="Insert" CellPadding="4" ForeColor="#333333" GridLines="None" OnItemInserting="DetailsView_ItemInserting"&gt;
          &lt;Fields&gt;
              &lt;asp:TemplateField HeaderText="Nombre *"&gt;
                  &lt;InsertItemTemplate&gt;
                      &lt;asp:TextBox ID="txtNombre" runat="server" Text='&lt;%# Bind("nombre) %&gt;'&gt;&lt;/asp:TextBox&gt;
                      &lt;asp:RequiredFieldValidator ID="requireNombre" runat="server" ControlToValidate="txtNombre" ErrorMessage="El campo 'Nombre' no puede estar vacío." ValidationGroup="DetailsGroup" Display="None"&gt;&lt;/asp:RequiredFieldValidator&gt;
                      &lt;asp:CustomValidator id="CustomValidator1" ControlToValidate="txtNombre" ClientValidationFunction="validateNombre" ValidationGroup="DetailsGroup" Display="none" ErrorMessage="Introduzca un nombre diferente." runat="server"/&gt;
                  &lt;/InsertItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
          &lt;/Fields&gt;
      &lt;/asp:DetailsView&gt;
  </code></pre>
  
  <p>Function AJAX:</p>
  
  <pre><code>        function validateNombre(src, args) {
          var isValid;
          $.ajax({
              type: "POST",
              url: "Nombre.aspx/ComprobarNombre",
              data: "{'nombre': '" + args.Value + "'}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              async: false,
              success: function (msg) {
                  isValid = msg.d;
              }
          });
          args.IsValid = isValid;
  </code></pre>
  
  <p>ComprobarNombre:
  [WebMethod()]</p>
  
  <pre><code>    public static bool ComprobarNombre(string nombre)
          {
              /* SQLConnections... */
  
              if(...)
                   return false;
              else
                   return true;
          }
  </code></pre>
  
  <p>The problem is that the AJAX function is launched when every time I enter a value in the TextBox. I need that the function launch only when I click the button of the form.</p>