Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ValidatorValidate() validates all the RequiredFieldValidator controls on the page?

In following code Why ValidatorValidate(v) validates all the RequiredFieldValidator controls on the page? It should execute only RequiredFieldValidator1 not RequiredFieldValidator2.
Here is code.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/javascript">
        function check() {

            var v = document.getElementById("<%=RequiredFieldValidator1.ClientID%>");
            ValidatorValidate(v);

        }
        </script>    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>

            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
            <br />
            <asp:Button ID="Button1" runat="server" OnClientClick="check()" Text="Check" />

        </div>

        </form>
</body>
</html>
like image 820
jams Avatar asked Dec 29 '25 06:12

jams


2 Answers

You need to return something from check(), otherwise, it's running it, and then passing through and doing the normal page validation.

After calling ValidatorValidate(), you can check if the validator isvalid

function check() {

        var v = document.getElementById("<%=RequiredFieldValidator1.ClientID%>");
        ValidatorValidate(v);
if (v.isvalid)
     return true;
else
     return false;
}

You did have an extra } in there as well.

You also need to throw in a return for the OnClientClick

<asp:Button ID="Button1" runat="server" OnClientClick="return check()" Text="Check" />
like image 77
Doozer Blake Avatar answered Dec 30 '25 18:12

Doozer Blake


This happens because once you click the Button, it causes validation of all of them on postback. You'll need to group them by ValidationGroup or use return false; from check() to stop the postback.

Alternatively you could also replace the RequiredFieldValidator with CustomValidator and do conditional checks based on your needs.

If you REALLY want to do client side validator handling, check out http://msdn.microsoft.com/en-us/library/yb52a4x0.aspx#ClientSideValidation_ClientValidationObjectModel

This page has details on Client Validation Object Model which has a few JavaScript functions to handle conditional evaluation. Check out Validation event for asp net client side validation for an example of what one person was doing along these lines.

What are you trying to do specifically? Someone can probably help you get the correct setup.

like image 22
Kirk Avatar answered Dec 30 '25 20:12

Kirk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!