简体   繁体   中英

ASP.NET - Validating a CustomValidator

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;
        }
    }

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");
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM