[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Custom Validator not working

Posted on 2008-02-06
9
Medium Priority
?
950 Views
Last Modified: 2012-05-05
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;
        else
          args.IsValid = false;
    }
//----------------------------------------------
 
//NOTE:
//
//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:ListBox>
        <asp:Label ID="Label1" runat="server" Style="z-index: 103; left: 16px; position: absolute;
            top: 9px" Text="Registered Pupils:"></asp:Label>
        &nbsp;
        <asp:Panel ID="pnlRegData" runat="server" Font-Bold="True" Font-Size="Medium"
            Height="401px" Style="z-index: 102; left: 228px; position: absolute; top: -21px"
            Width="761px">            
            <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">
            </asp:DropDownList>
            <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:DropDownList>
                <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:"
                    Width="695px"></asp:Label>
            </asp:Panel>
            <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"
                    Width="726px">
                    &nbsp;
                    <asp:DropDownList ID="ddlPrevPupils" runat="server" Style="z-index: 100; left: 214px;
                        position: absolute; top: 15px" Width="195px" AutoPostBack="True" TabIndex="5">
                    </asp:DropDownList>
                    <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>
                    &nbsp;
                    <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>
                    &nbsp;
                    <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>
                <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 />
                    &nbsp;
                    <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>
                    &nbsp;&nbsp;
                    <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 />
                </asp:Panel>
                &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 />
                </asp:Panel>
                <br />
                <br />
                <br />
            </asp:Panel>
            &nbsp;
            <br />
            <br />
        </asp:Panel>
        <asp:HiddenField ID="hfControlName" runat="server" />
        <asp:HiddenField ID="hfNeedToConfirmUpdate" runat="server" />
        <asp:HiddenField ID="hfSelectedValue" runat="server" />
    </div>
    <br />
</asp:Content>

Open in new window

0
Comment
Question by:allanmark
  • 6
  • 3
9 Comments
 
LVL 8

Expert Comment

by:Yiogi
ID: 20834199
Do you have causesvalidation property set to true in your textbox? From what I see in the above code you don't have it.
0
 

Author Comment

by:allanmark
ID: 20834589
Wasn't set.  Iset it and re-ran --  still NO validation.
0
 

Author Comment

by:allanmark
ID: 20834744
Maybe I should have inlcuded this:

I'm using MasterPages, with the buttons being on the MasterPage.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:allanmark
ID: 20835576
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) ??
0
 
LVL 8

Expert Comment

by:Yiogi
ID: 20836262
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:
http://aspnet.4guysfromrolla.com/articles/073102-1.aspx

It also has a working example. Hope it helps you.
0
 

Author Comment

by:allanmark
ID: 20838752
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

0
 
LVL 8

Accepted Solution

by:
Yiogi earned 1400 total points
ID: 20839095
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?
0
 

Author Comment

by:allanmark
ID: 20839464
Perfect!

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!!!
0
 

Author Closing Comment

by:allanmark
ID: 31428567
Excellent support! Thank You!!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question