title: ASP.NET - Validating a CustomValidator
body: <p>I'm trying to get a CustomValidator to call a server method (it's a checksum, I think it's right but as you'll see I can't really tell) but it's never called, I can enter anything into the text box and the error message never appears, and adding a break-point in the method reveals that it's never called at all not matter what's entered in the text box. Do I need client-side validation too?</p>
  
  <p>My OnServerValidate looks to be calling the method correctly to my eyes - I really am at a loss here. Help much appreciated!</p>
  
  <p>The ASP.Net code:</p>
  
  <pre><code>   &lt;asp:Label ID="lblPPSN" runat="server" Text="PPS number: "&gt;&lt;/asp:Label&gt;
      &lt;asp:TextBox ID="tbxPPSN" runat="server"&gt;&lt;/asp:TextBox&gt;
      &lt;asp:CustomValidator ID="customval_PPSN" runat="server" ErrorMessage="Please enter a valid PPSN" ControlToValidate="tbxPPSN"
          OnServerValidate="customval_PPSN_ServerValidate" ForeColor="Red"&gt;&lt;/asp:CustomValidator&gt;
  </code></pre>
  
  <p>The c# code-behind:</p>
  
  <pre><code> protected void customval_PPSN_ServerValidate(object source, ServerValidateEventArgs args)
      {
          string checkChar = "", formatChar = "", input = tbxPPSN.Text;
          bool newFormat = false;
  
          if (input.Length &gt; 9)
          {
              args.IsValid = false;
              return;
          }
  
          if (input.Length == 9)
          {
              newFormat = true;
          }
  
          char[] PPSArray = input.ToCharArray();
          Array.Reverse(PPSArray);
          checkChar = PPSArray[0].ToString();
  
          if (newFormat)
          {
              checkChar = PPSArray[1].ToString();
              formatChar = PPSArray[0].ToString();
          }
  
          checkChar = checkChar.ToLower();
          formatChar = formatChar.ToLower();
  
          int seed = 2, PPSParsed = 0, PPSsum = 0, PPSMod = 0;
  
          foreach (char ppschar in PPSArray)
          {
              if (int.TryParse(ppschar.ToString(), out PPSParsed))
              {
                  PPSsum += PPSParsed * seed;
                  seed++;
              }
          }
  
          if (newFormat)
          {
              PPSsum += Convert.ToInt32(formatChar.ToCharArray()[0] - 96) * 9;
          }
  
          PPSMod = PPSsum % 23;
  
          if (PPSMod == 0) PPSMod = 23;
  
          if (Convert.ToString((char) (96 + PPSMod)).ToLower() == checkChar)
          {
              args.IsValid = true;
              return;
          }
          else
          {
              args.IsValid = false;
          }
      }
  </code></pre>