Custom validator in asp.net

Dinesh Kumar
Dinesh Kumar used Ask the Experts™
on
I have created a custom validator which checks for even number.
so what happens at first at button click is:

1. CustomValidator1_ServerValidate is called.
2. and then Button1_Click is called.

in 2 step:
   protected void Button1_Click(object sender, EventArgs e)
    {
       if (CustomValidator1.IsValid) // this is always true even if  i have entered odd number and customvalidor1.isvalid is set to false
        {
           
        }
    }




Thanks
Dinesh Kumar
ASPX Code:
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
              
                    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator"
                        ControlToValidate="TextBox1"  OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

ASPX.CS Code:

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (CustomValidator1.IsValid) // this is always true why???
        {
            
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (Convert.ToInt32(args.Value) % 2 == 0)
        {
            CustomValidator1.IsValid = true;
            Label1.Text = "Validaton Passed";
        }
        else
        {
            CustomValidator1.IsValid = false;
            Label1.Text = "Sorry, please enter even numbers only";           
        }
    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try Checking with Page.IsValid Property for server controls on button click

"As soon as you place a validation control on a page, it is imperative that you check the IsValid flag of the Page class before using any of the data posted by the client.  It is a common misconception that if validation fails on a page, the code for that page will not execute.  On the contrary, the only thing that happens when server-side validation fails is that the IsValid flag of the Page class is set to false, and each validation control that failed renters itself as a visible span so that the error indicator shows up when the page is redisplayed to the user."

Refer to
When using ASP.NET Validator Controls... if (Page.IsValid) { Now it's safe to go on! }

How to: Test Validity Programmatically for ASP.NET Server Controls  

Author

Commented:
Hi, if you try to run the code, you will see that Page.IsValid property is also not working.
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Hi!
You set args.IsValid in server validation function, not validator.IsValid:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (Convert.ToInt32(args.Value) % 2 == 0)
        {
            args.IsValid = true;
            Label1.Text = "Validaton Passed";
        }
        else
        {
            args.IsValid = false;
            Label1.Text = "Sorry, please enter even numbers only";          
        }
    }

Author

Commented:
great ivan,

the code is working. but args.IsValid why?

args just refers to the value in the textbox or its something more?

please explain.
This is how server side validation function works:
it sets IsValid property of validator (CustomValidator1.IsValid)  to the value of args.IsValid after CustomValidator1_ServerValidate is completed, so you need to need initialize value in event arguments to make it work.

Author

Commented:
And can you please look into what is wrong i am doing with the new attached code. it does not print the error message when i use clientvalidationfunction?
<script language="javascript" type="text/javascript">
        function CheckEvenNumber(source, arguments) {
            var number = document.getElementById('TextBox1').value;
            number = number % 2;
            if (number == 0) {
                arguments.IsValid = true;
            }
            else {
                arguments.Isvalid = false;
            }
        }

    </script>


<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Not an Even Number"                        ControlToValidate="TextBox1" ClientValidationFunction="CheckEvenNumber" ></asp:CustomValidator>

Open in new window

Author

Commented:
and FYI i am having the textbox1 exists on the page :-)
You havw wrong case for arguments.Isvalid = false;
It should be arguments.IsValid = false;

Author

Commented:
Thanks Boss.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial