Custom Validator not working

Greetings all

I have implemented a custom validator on a web page (in a nutshell, it checks a textbox to ensure that only 0 .. 9 was entered). There is alsoa requiredfield validator on the Textbox.

I first created a testpage and once that was working, duplicated the necessary code - except it isn't working.

Any pointers?

In advance, thanks!!
    protected void ID_Numeric_ServerValidation(object source, ServerValidateEventArgs args)
        Match m = Regex.Match(txtID.Text, "^[0-9]{1,}$");
        if (m.Success)
          args.IsValid = true;
          args.IsValid = false;
//txtID  = the Textbox
//rvcIDNum = the custom validator
<%@ Page Language="C#" MasterPageFile="~/MasterPage_03_Update_DB.master" AutoEventWireup="true" CodeFile="Pupil_Register.aspx.cs" Inherits="Pupil_Register" Title="Registration" %>
<%@ MasterType  virtualPath="~/MasterPage_03_Update_DB.master"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div style="z-index: 100; left: 8px; width: 940px; position: absolute; top: 123px;
        height: 383px">
        <asp:ListBox ID="lbxRegistrations" runat="server" BackColor="SeaShell" Height="314px" Style="z-index: 100;
            left: 14px; position: absolute; top: 30px" Width="195px" OnSelectedIndexChanged="lbxRegistrations_SelectedIndexChanged" TabIndex="2">
        <asp:Label ID="Label1" runat="server" Style="z-index: 103; left: 16px; position: absolute;
            top: 9px" Text="Registered Pupils:"></asp:Label>
        <asp:Panel ID="pnlRegData" runat="server" Font-Bold="True" Font-Size="Medium"
            Height="401px" Style="z-index: 102; left: 228px; position: absolute; top: -21px"
            <br />
            <asp:Label ID="Label2" runat="server" Font-Bold="True" Style="z-index: 101; left: -193px;
                position: absolute; top: 6px" Text="Year:"></asp:Label>
            <br />
            <asp:DropDownList ID="ddlRegYear" runat="server" Font-Bold="True" Font-Size="Medium"
                Style="z-index: 102; left: -137px; position: absolute; top: 1px" Width="93px" OnSelectedIndexChanged="ddlRegYear_SelectedIndexChanged" AutoPostBack="True" TabIndex="1">
            <br />
            <br />
            <br />
            <br />
            <asp:Panel ID="pnlGradeFees" runat="server" BackColor="#FFC080" Height="53px" Style="z-index: 103;
                left: 29px; position: absolute; top: 7px" Width="692px">
                <asp:Label ID="Label3" runat="server" Font-Size="Smaller" Style="z-index: 100; left: 24px;
                    position: absolute; top: 9px" Text="Grade:"></asp:Label>
                <asp:DropDownList ID="ddlGrades" runat="server" Style="z-index: 101; left: 73px; position: absolute;
                    top: 5px" Width="142px" OnSelectedIndexChanged="ddlGrade_SelectedIndexChanged" AutoPostBack="True" TabIndex="3">
                <asp:Label ID="lblFees" runat="server" Font-Bold="False" Font-Size="X-Small" Style="z-index: 103;
                    left: 8px; position: absolute; top: 33px" Text="Fees:"
            <asp:Panel ID="pnlPupilData" runat="server" Font-Bold="False" Font-Size="Smaller"
                GroupingText=" REGISTRATION DETAILS " Height="317px" Style="z-index: 104; left: 8px;
                position: absolute; top: 68px" Width="748px" Enabled="False">
                <asp:CheckBox ID="chkOverrideVal" runat="server" Font-Bold="False" Font-Size="Smaller"
                    Style="z-index: 100; left: 600px; position: absolute; top: 6px" Text="Override validation?"
                    TextAlign="Left" AutoPostBack="True" OnCheckedChanged="chkOverrideVal_CheckedChanged" />
                <br />
                <br />
                <br />
                <br />
                <asp:Panel ID="pnlPupil" runat="server" Font-Bold="True" GroupingText=" Pupil "
                    Height="62px" Style="z-index: 101; left: 15px; position: absolute; top: 22px"
                    <asp:DropDownList ID="ddlPrevPupils" runat="server" Style="z-index: 100; left: 214px;
                        position: absolute; top: 15px" Width="195px" AutoPostBack="True" TabIndex="5">
                    <asp:Button ID="btnUsePrev" runat="server" BackColor="#FFC080" Font-Bold="True" ForeColor="Blue"
                        Height="22px" Style="z-index: 101; left: 416px; position: absolute; top: 14px"
                        Text="Use existing details" OnClick="btnUsePrev_Click" CausesValidation="False" TabIndex="6" />
                    <asp:CheckBox ID="chkShowPrevPupils" runat="server" Checked="True" Font-Bold="False"
                        Font-Size="Small" Style="z-index: 102; left: 20px; position: absolute; top: 17px"
                        Text="Show last year's pupils?" TextAlign="Left" OnCheckedChanged="chkShowPrevPupils_CheckedChanged" AutoPostBack="True" TabIndex="4" />
                    <br />
                    <asp:Label ID="Label6" runat="server" Font-Bold="False" Style="z-index: 103; left: 76px;
                        position: absolute; top: 58px" Text="Name:"></asp:Label>
                    <asp:TextBox ID="txtName" runat="server" Style="z-index: 104; left: 122px; position: absolute;
                        top: 52px" Width="95px" TabIndex="7"></asp:TextBox>
                    <asp:Label ID="Label7" runat="server" Font-Bold="False" Style="z-index: 105; left: 248px;
                        position: absolute; top: 58px" Text="Surname:"></asp:Label>
                    <asp:TextBox ID="txtSurname" runat="server" Style="z-index: 106; left: 314px; position: absolute;
                        top: 52px" Width="122px" TabIndex="8"></asp:TextBox>
                    <asp:Label ID="Label8" runat="server" Font-Bold="False" Style="z-index: 107; left: 469px;
                        position: absolute; top: 58px" Text="ID No.:"></asp:Label>
                    <asp:TextBox ID="txtID" runat="server" Style="z-index: 114; left: 515px; position: absolute;
                        top: 52px" TabIndex="9" Width="105px"></asp:TextBox>
                    <asp:CustomValidator ID="rvcCheckID" runat="server" ErrorMessage="Registration duplicated"
                        Style="z-index: 109; left: 639px; position: absolute; top: 13px" ControlToValidate="txtID" Font-Bold="True" OnServerValidate="ID_ServerValidation" SetFocusOnError="True">Registration duplicated</asp:CustomValidator>
                    <br />
                    <asp:RequiredFieldValidator ID="rvPupilName" runat="server" ControlToValidate="txtName"
                        ErrorMessage="*" Font-Bold="False" SetFocusOnError="True" Style="z-index: 110;
                        left: 227px; position: absolute; top: 56px">*</asp:RequiredFieldValidator>
                    <asp:RequiredFieldValidator ID="rvPupilSurname" runat="server" ControlToValidate="txtSurname"
                        ErrorMessage="*" Font-Bold="False" SetFocusOnError="True" Style="z-index: 111;
                        left: 446px; position: absolute; top: 56px">*</asp:RequiredFieldValidator>
                    <asp:RequiredFieldValidator ID="rvPupilID" runat="server" ControlToValidate="txtID"
                        ErrorMessage="*" Font-Bold="False" Style="z-index: 112; left: 630px; position: absolute;
                        top: 56px">*</asp:RequiredFieldValidator>
                    <asp:CustomValidator ID="rvcIDNum" runat="server" ErrorMessage="Non numeric"
                        Font-Bold="False" SetFocusOnError="True"
                        Style="z-index: 113; left: 649px; position: absolute; top: 56px" ValidateEmptyText="True" OnServerValidate="ID_Numeric_ServerValidation" ControlToValidate="txtID">Non numeric</asp:CustomValidator>
                    <br />
                <asp:Panel ID="pnlParental" runat="server" GroupingText=" Parental " Height="139px"
                    Style="z-index: 102; left: 15px; position: absolute; top: 106px" Width="726px" Font-Bold="True">
                    <asp:Label ID="Label18" runat="server" Font-Bold="False" Style="z-index: 100; left: 11px;
                        position: absolute; top: 21px" Text="FATHER:"></asp:Label>
                    <asp:Label ID="Label19" runat="server" Font-Bold="False" Style="z-index: 101; left: 12px;
                        position: absolute; top: 66px" Text="MOTHER:"></asp:Label>
                    <asp:Label ID="Label20" runat="server" Font-Bold="False" Style="z-index: 102; left: 8px;
                        position: absolute; top: 112px" Text="CUSTODY:"></asp:Label>
                    <asp:Button ID="btnUseFather" runat="server" Height="20px" Style="z-index: 103; left: 524px;
                        position: absolute; top: 95px" Text="... is Father" Width="83px" OnClick="btnUseFather_Click" TabIndex="18" CausesValidation="False" />
                    <asp:Button ID="btnUseMother" runat="server" Height="20px" Style="z-index: 104; left: 524px;
                        position: absolute; top: 119px" Text="... is Mother" Width="83px" OnClick="btnUseMother_Click" TabIndex="19" CausesValidation="False" />
                    <asp:RequiredFieldValidator ID="rvFrName" runat="server" ControlToValidate="txtFrName"
                        ErrorMessage="*" SetFocusOnError="True" Style="z-index: 105; left: 226px; position: absolute;
                        top: 19px">*</asp:RequiredFieldValidator>
                    <asp:RequiredFieldValidator ID="rvFrSurname" runat="server" ControlToValidate="txtFrSurname"
                        ErrorMessage="*" SetFocusOnError="True" Style="z-index: 106; left: 446px; position: absolute;
                        top: 19px">*</asp:RequiredFieldValidator>
                    &nbsp;<br />
                    <asp:CustomValidator ID="rvcFrIDNum" runat="server" ControlToValidate="txtFrID" ErrorMessage="Non numeric"
                        Font-Bold="False" OnServerValidate="ID_Fr_Numeric_ServerValidation" SetFocusOnError="True"
                        Style="z-index: 107; left: 648px; position: absolute; top: 18px" ValidateEmptyText="True">Non numeric</asp:CustomValidator>
                    <br />
                    <asp:Label ID="Label12" runat="server" Font-Bold="False" Style="z-index: 108; left: 76px;
                        position: absolute; top: 66px" Text="Name:"></asp:Label>
                    <asp:TextBox ID="txtMotName" runat="server" Style="z-index: 109; left: 121px; position: absolute;
                        top: 60px" Width="95px" TabIndex="13"></asp:TextBox>
                    <asp:Label ID="Label13" runat="server" Font-Bold="False" Style="z-index: 110; left: 248px;
                        position: absolute; top: 66px" Text="Surname:"></asp:Label>
                    <asp:TextBox ID="txtMotSurname" runat="server" Style="z-index: 111; left: 313px;
                        position: absolute; top: 60px" Width="122px" TabIndex="14"></asp:TextBox>
                    <asp:Label ID="Label15" runat="server" Font-Bold="False" Style="z-index: 112; left: 76px;
                        position: absolute; top: 112px" Text="Name:"></asp:Label>
                    <asp:TextBox ID="txtCusName" runat="server" Style="z-index: 113; left: 121px; position: absolute;
                        top: 106px" Width="95px" TabIndex="16"></asp:TextBox>
                    <asp:Label ID="Label16" runat="server" Font-Bold="False" Style="z-index: 114; left: 251px;
                        position: absolute; top: 112px" Text="Surname:" Width="29px"></asp:Label>
                    <asp:TextBox ID="txtCusSurname" runat="server" Style="z-index: 115; left: 314px;
                        position: absolute; top: 106px" Width="122px" TabIndex="17"></asp:TextBox>
                    <asp:Label ID="Label14" runat="server" Font-Bold="False" Style="z-index: 116; left: 469px;
                        position: absolute; top: 66px" Text="ID No.:"></asp:Label>
                    <asp:TextBox ID="txtMotID" runat="server" Style="z-index: 117; left: 515px; position: absolute;
                        top: 60px" Width="105px" TabIndex="15"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rvFrID" runat="server" ControlToValidate="txtFrID"
                        ErrorMessage="*" SetFocusOnError="True" Style="z-index: 118; left: 630px; position: absolute;
                        top: 18px">*</asp:RequiredFieldValidator>
                    <asp:RequiredFieldValidator ID="rvMotID" runat="server" ControlToValidate="txtMotID"
                        ErrorMessage="*" SetFocusOnError="True" Style="z-index: 119; left: 630px; position: absolute;
                        top: 63px">*</asp:RequiredFieldValidator>
                    <br />
                    <asp:RequiredFieldValidator ID="rvMotName" runat="server" ControlToValidate="txtMotName"
                        ErrorMessage="*" SetFocusOnError="True" Style="z-index: 120; left: 226px; position: absolute;
                        top: 64px">*</asp:RequiredFieldValidator>
                    <asp:RequiredFieldValidator ID="rvMotSurname" runat="server" ControlToValidate="txtMotSurname"
                        ErrorMessage="*" SetFocusOnError="True" Style="z-index: 121; left: 445px; position: absolute;
                        top: 64px" Width="13px">*</asp:RequiredFieldValidator>
                    <asp:CustomValidator ID="rvcMotIDNum" runat="server" ControlToValidate="txtMotID"
                        ErrorMessage="Non numeric" Font-Bold="False" OnServerValidate="ID_Mot_Numeric_ServerValidation"
                        SetFocusOnError="True" Style="z-index: 131; left: 649px; position: absolute;
                        top: 63px">Non numeric</asp:CustomValidator>
                    <br />
                    <asp:Label ID="Label9" runat="server" Style="z-index: 123; left: 76px; position: absolute;
                        top: 21px" Text="Name:" Font-Bold="False"></asp:Label>
                    <asp:TextBox ID="txtFrName" runat="server" Style="z-index: 124; left: 121px; position: absolute;
                        top: 15px" Width="95px" TabIndex="10"></asp:TextBox>
                    <asp:Label ID="Label10" runat="server" Style="z-index: 125; left: 248px; position: absolute;
                        top: 21px" Text="Surname:" Font-Bold="False"></asp:Label>
                    <asp:TextBox ID="txtFrSurname" runat="server" Style="z-index: 126; left: 313px; position: absolute;
                        top: 15px" Width="122px" TabIndex="11"></asp:TextBox>
                    <asp:Label ID="Label11" runat="server" Style="z-index: 127; left: 469px; position: absolute;
                        top: 21px" Text="ID No.:" Font-Bold="False"></asp:Label>
                    <asp:TextBox ID="txtFrID" runat="server" Style="z-index: 128; left: 515px; position: absolute;
                        top: 15px" Width="105px" TabIndex="12"></asp:TextBox>
                    <br />
                    <br />
                    <asp:RequiredFieldValidator ID="rvCusName" runat="server" ControlToValidate="txtCusName"
                        ErrorMessage="*" SetFocusOnError="True" Style="z-index: 129; left: 226px; position: absolute;
                        top: 111px">*</asp:RequiredFieldValidator>
                    <asp:RequiredFieldValidator ID="rvCusSurname" runat="server" ControlToValidate="txtCusSurname"
                        ErrorMessage="*" SetFocusOnError="True" Style="z-index: 130; left: 447px; position: absolute;
                        top: 110px">*</asp:RequiredFieldValidator>
                    <br />
                &nbsp; &nbsp;&nbsp;&nbsp;
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <asp:Panel ID="pnlAdmin" runat="server" GroupingText=" Administrative " Height="50px"
                    Style="z-index: 104; left: 18px; position: absolute; top: 260px" Width="720px">
                    <asp:CheckBox ID="chkAllDocComp" runat="server" Font-Size="Smaller" Style="z-index: 100;
                        left: 21px; position: absolute; top: 20px" Text="All documents completed?" TextAlign="Left" TabIndex="20" />
                    <asp:CheckBox ID="chkAcceptResp" runat="server" Font-Size="Smaller" Style="z-index: 101;
                        left: 207px; position: absolute; top: 20px" Text="Accepted financial responsability?"
                        TextAlign="Left" TabIndex="21" />
                    <asp:CheckBox ID="chkOverrideReq" runat="server" Font-Size="Smaller" Style="z-index: 103;
                        left: 433px; position: absolute; top: 20px" Text="Override Reg. requirements?"
                        TextAlign="Left" TabIndex="22" />
                    <br />
                <br />
                <br />
                <br />
            <br />
            <br />
        <asp:HiddenField ID="hfControlName" runat="server" />
        <asp:HiddenField ID="hfNeedToConfirmUpdate" runat="server" />
        <asp:HiddenField ID="hfSelectedValue" runat="server" />
    <br />

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Do you have causesvalidation property set to true in your textbox? From what I see in the above code you don't have it.
allanmarkAuthor Commented:
Wasn't set.  Iset it and re-ran --  still NO validation.
allanmarkAuthor Commented:
Maybe I should have inlcuded this:

I'm using MasterPages, with the buttons being on the MasterPage.
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

allanmarkAuthor Commented:
Another update (progress?).

I added a button directly to the Contents page. Whenthis is clicked the custom validator kicks in, provided that all the text boxes have data in them.

This leads to 2 questions:

1.  Why does it work undr this condition ??
2.  Is there a way to enforce the custom validation, if all fields aren't entered (as long as the specific field
    has data) ??
Have you written client side script for your validator? Custom validators do not work like other validators. They only perform server side validation so they will only kick in if you perform a postback. If you want them to be working at runtime you'll have to write the script for that.

This side explains how to use a custom validator:

It also has a working example. Hope it helps you.
allanmarkAuthor Commented:
An excellent article! I have bookmarked for future reference. At this stage, serverside validation is what I need.

What I have discovered is that in my Save button click I was disabling the validators (see snippet) - in a nut shell, old data may have missing fields and unless the user edits, I want to leave that alone. So, I commented that call out - now the validators kick in and display the error message, but the record is still saved and the user returns to the starting point again. That brings me to:

1.  When do the custom validators kick in (after Postback?judging from what is happening, it looks like the
     Save button  code executes first) ?
2.  Do Custom validators "prevent" teh code behind the button from executiong (as does a requried field
    validator) ?

 protected void Master_LocalUpdate(object sender, EventArgs e)
     ValidationDisable();     // Disables validators so that when browsing
     .............            // existign data, with missing fields, it
     .............            // won't flash up.

Open in new window

Hi allan,

Ok to address your questions the custom validation will kick in before postback. It will occur before your button actually goes to the button click event, or whatever you did that had autopostback enabled goes there.

They do NOT prevent code behind from executing no. You will need in your code to check if the Page.IsValid property is true before proceeding by yourself.

I didn't notice it before but I just saw on your original post that you are simply validating a regular expression. Have you thought of using a regularexpressionvalidator instead?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
allanmarkAuthor Commented:

I used the Page.Isvalid nd that solve dthe problem. Then, for fun, I used the RegularExpressionValidator - and want to kick myself - it's so simple and I've wasted hours!!!

Thanks, very much for the help!!!
allanmarkAuthor Commented:
Excellent support! Thank You!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.