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

依然范特西╮ 提交于 2020-01-24 09:06:12

问题


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>

回答1:


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" />



回答2:


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.




回答3:


Your script is malformed.

<head>
    <script type="text/javascript">
         function check() {
             var v = document.getElementById("<%=RequiredFieldValidator1.ClientID%>");
             ValidatorValidate(v);
         }
   </script>    
</head>

In your version you are getting a javascript error that the function check is not defined.

The second validator gets also triggered because Validator always are triggered before postback and your function check is called on a submit-button. Both validators would validate anyway even without your explicit call on ValidatorValidate.

If you don't want to postback onclick, use a HtmlButton instead:

<input type="button" onclick="check()" value="Check" />


来源:https://stackoverflow.com/questions/7640426/why-validatorvalidate-validates-all-the-requiredfieldvalidator-controls-on-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!