javascript 为什么 ValidatorValidate() 验证页面上的所有 RequiredFieldValidator 控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7640426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:46:17  来源:igfitidea点击:

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

javascriptasp.netrequiredfieldvalidatorvalidation-controls

提问by jams

In following code Why ValidatorValidate(v)validates all the RequiredFieldValidatorcontrols on the page? It should execute only RequiredFieldValidator1not RequiredFieldValidator2.
Here is code.

在下面的代码中为什么要ValidatorValidate(v)验证RequiredFieldValidator页面上的所有控件?它应该只执行RequiredFieldValidator1not RequiredFieldValidator2
这是代码。

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

回答by Doozer Blake

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

你需要从 check() 返回一些东西,否则,它正在运行它,然后通过并进行正常的页面验证。

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

调用ValidatorValidate()后,可以检查validator是否有效

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

您还需要为 OnClientClick 投入回报

<asp:Button ID="Button1" runat="server" OnClientClick="return check()" Text="Check" />

回答by Kirk

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

发生这种情况是因为一旦您单击按钮,它会在回发时对所有这些按钮进行验证。您需要将它们分组ValidationGroup或使用return false;fromcheck()来停止回发。

Alternatively you could also replace the RequiredFieldValidatorwith CustomValidatorand do conditional checks based on your needs.

或者,您也可以根据需要替换RequiredFieldValidatorwithCustomValidator并进行条件检查。

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

如果您真的想进行客户端验证器处理,请查看http://msdn.microsoft.com/en-us/library/yb52a4x0.aspx#ClientSideValidation_ClientValidationObjectModel

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

此页面包含有关客户端验证对象模型的详细信息,该模型具有一些处理条件评估的 JavaScript 函数。请查看ASP 网络客户端验证的验证事件,以了解一个人在这些方面所做的事情的示例。

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

你具体想做什么?有人可能会帮助您获得正确的设置。

回答by Tim Schmelter

Your scriptis malformed.

script的格式不正确。

<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 checkis not defined.

在您的版本中,您收到一个 javascript 错误,指出该函数check未定义。

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

第二个验证器也会被触发,因为 Validator 总是在回发之前触发,并且您的函数check在提交按钮上被调用。即使没有您显式调用ValidatorValidate.

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

如果您不想回发 onclick,请改用 HtmlButton:

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