1

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;
        }
    }
1
  • Hm, it looks valid at first glance. Are you sure it's not being hit? Are you debugging this in Visual Studio, and if so can you try a 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. Commented Nov 1, 2014 at 14:46

1 Answer 1

1

If you never post back to the server your server side validation will not get called. You may want to put in some client side validation so your users don't need to submit the form to get their validation results.

This MSDN article has an example of using both server side and client side validation: CustomValidator Example

I used your markup and code-behind and it seems to work.

Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Val.aspx.cs" Inherits="CustomValidatorExample.Val" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <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>
            <asp:Button runat="server" ID="button1" Text="Validate" OnClick="button1_Click" />
        </div>
    </form>
</body>
</html>

Code-behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomValidatorExample
{
    public partial class Val : System.Web.UI.Page
    {
        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;
            }
        }

        protected void button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Response.Write("Valid");
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I've got a summit button that when clicked checks If (Page.IsValid) - shouldn't that handle what I want to do correctly?
@user2966419 Page.IsValid only checks the results of the validation, it does not perform the validation. The ServerValidate event handler will get called prior to the button's Click handler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.