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?
My OnServerValidate looks to be calling the method correctly to my eyes - I really am at a loss here. Help much appreciated!
The ASP.Net code:
<asp:Label ID="lblPPSN" runat="server" Text="PPS number: "></asp:Label>
<asp:TextBox ID="tbxPPSN" runat="server"></asp:TextBox>
<asp:CustomValidator ID="customval_PPSN" runat="server" ErrorMessage="Please enter a valid PPSN" ControlToValidate="tbxPPSN"
OnServerValidate="customval_PPSN_ServerValidate" ForeColor="Red"></asp:CustomValidator>
The c# code-behind:
protected void customval_PPSN_ServerValidate(object source, ServerValidateEventArgs args)
{
string checkChar = "", formatChar = "", input = tbxPPSN.Text;
bool newFormat = false;
if (input.Length > 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;
}
}
System.Diagnostics.Debug.WriteLine()at the start of the method to see if it outputs anything to the Output window? Do you have any ValidationGroups set up on your button or form elements that might cause this validator to be ignored? You shouldn't need client-side validation; that's just a convenience option.