Search code examples
c#webformscustomvalidator

Custom Validation object is not set to an instance


I'm new to validation and I have crated a simple webform that I am trying to validate, where I override the EvaluateIsValid() method from the BaseValidator class in the App_Code folder. In this method I am getting the error Object is not set to an instance of an object at the this.GetControlValidationValue() call to set the string.

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

<!DOCTYPE html>

<script runat="server">
    protected void pageCvd_ServerValidate(object source, ServerValidateEventArgs args)
    {
        ValidApp.App_Code._Validators valid = new ValidApp.App_Code._Validators(usernameTxt.ClientID.ToString());
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="display:inline-block; width:200px;">
        <asp:CustomValidator ID="pageValidatorCvd" runat="server" EnableClientScript="false" OnServerValidate="pageCvd_ServerValidate"></asp:CustomValidator>       
        <asp:Label ID="usernameLbl" runat="server" Text="Username:" style="width:50px; font-size:xx-small"></asp:Label>
        <asp:TextBox ID="usernameTxt" runat="server" style="width:100px; font-size:xx-small"></asp:TextBox>
        <br />
        <asp:Label ID="passwordLbl" runat="server" Text="Password:" style="width:50px; font-size:xx-small"></asp:Label>
        <asp:TextBox ID="passwordTxt" runat="server" style="width:100px; font-size:xx-small"></asp:TextBox> 
    </div>
    <div>
        <asp:Button ID="registryBtn" runat="server" OnCommand="registryBtn_Command" CommandName="" CommandArgument="" Text="Register" style="width:50px; font-size:xx-small"/>
    </div>
    </form>
</body>
</html>

Code Behind


namespace ValidApp.App_Code
{
    public class _Validators : BaseValidator
    {
        private string regex;
        private string errorMessage;
        private string controlToValidate;

        protected override bool EvaluateIsValid()
        {
            string value = this.GetControlValidationValue(controlToValidate);
            return Regex.IsMatch(value, regex);
        }

        public _Validators(string controlToValidate = "", string regex = @"^[a-z0-9]{6-8}$", string errorMessage = "The Username is not valid!")
        {
            this.regex = regex;
            this.errorMessage = errorMessage;
            this.controlToValidate = controlToValidate;

            bool validation = EvaluateIsValid();            
        }
    }
}

I have tried to create my own custom validation on <%@ Register > but to no avail.

Any ideas where I am going wrong?


Solution

  • The solution:

    It seems that you cannot make calls to the class constructor, that way EvaluateIsValid() method is hit on its own and the gets the right control!

    namespace ValidApp.App_Code
    {
        public class _Validators : BaseValidator
        {   
            protected override bool EvaluateIsValid()
            {
                string value = this.GetControlValidationValue(ControlToValidate);
                return Regex.IsMatch(value, @"^[a-z0-9]{6-8}$");
            }
    
            public _Validators()
            {
    
            }
        }
    }
    

    And that is all there is to it!