javascript asp网络客户端验证的验证事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7382849/
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-25 23:56:16  来源:igfitidea点击:

Validation event for asp net client side validation

javascriptasp.netvalidation

提问by Milox

I wonder if there's a way to hookup a custom function to asp net client-side validation event, so every time validation is fired by any control I can make some magic on client-side UI

我想知道是否有办法将自定义函数连接到 asp net 客户端验证事件,所以每次验证被任何控件触发时,我都可以在客户端 UI 上做一些魔术

I'm looking for a general approach to intercept page onvalidating event without setting it on every control that causes a postback

我正在寻找一种通用方法来拦截页面 onvalidating 事件,而无需在导致回发的每个控件上设置它

Thank you guys

感谢你们

Edit:

编辑:

I ended up with this function: (thanks to @Kirk)

我最终得到了这个函数:(感谢@Kirk)

$(document).ready(function () {
    $('form').submit(function () {
        if (typeof Page_Validators != 'undefined') {
            var errors = '';
            $.each(Page_Validators, function () {
                if (!this.isvalid) {
                    errors += this.errormessage + '\r\n';
                }
            });
            if (errors.length > 0) {
                Alert(errors);
            }
        }
    });    
}); 

采纳答案by Kirk

To do something along the lines of this you can place an OnClientClick event on the submit button or just the general form submission event.

为了做一些类似的事情,你可以在提交按钮上放置一个 OnClientClick 事件,或者只是一般的表单提交事件。

Then you can use the Client Validation Object Modelwith the validator controls. This actually allows you to verify each of the validation controls you've setup. There are a couple of values you can check against from the client relating to the page, see http://msdn.microsoft.com/en-us/library/yb52a4x0.aspx#ClientSideValidation_ClientValidationObjectModel.

然后您可以将Client Validation Object Model与验证器控件一起使用。这实际上允许您验证您设置的每个验证控件。您可以从与页面相关的客户端检查几个值,请参阅http://msdn.microsoft.com/en-us/library/yb52a4x0.aspx#ClientSideValidation_ClientValidationObjectModel

You reference each control with isvalidproperty. For example

您使用isvalid属性引用每个控件。例如

<asp:Label id="lblZip" runat="server" Text="Zip Code:" />
<asp:TextBox id="txtZip" runat="server" /></asp:TextBox>
<asp:RegularExpressionValidator id="valZip" runat="server"
   ControlToValidate="txtZip"
   ErrorMessage="Invalid Zip Code" 
   ValidationExpression="[0-9]{5}" />

<script language=javascript>
// Call this function to do something
function txtZipOnChange() {
   // Do nothing if client validation is not active
   if (typeof(Page_Validators) == "undefined")  return;
       // Change the color of the label
       lblZip.style.color = valZip.isvalid ? "Black" : "Red";
}
</script>

You can also get an array of the validators on the page with the client function Page_Validators. There are a few more functions you can use.

您还可以使用客户端函数获取页面上的验证器数组Page_Validators。您还可以使用更多功能。

Also you may use the ValidatorValidate(val)client function to force a check of each one indivually as well as ValidatorEnable(val, enable)to enable or disable validators as your logic demands.

此外,您还可以使用ValidatorValidate(val)客户端功能来强制单独检查每个ValidatorEnable(val, enable)验证器,以及根据您的逻辑要求启用或禁用验证器。

Take a look at http://msdn.microsoft.com/en-us/library/aa479045.aspx#aspplusvalid_clientsidefor a bit more detail.

查看http://msdn.microsoft.com/en-us/library/aa479045.aspx#aspplusvalid_clientside了解更多详细信息。

Hopefully this gets you where you need to go. If not, feel free to ask.

希望这能让你到达你需要去的地方。如果没有,请随时询问。

Previous CommentYou can use an onClientClickand attach a JavaScript function. http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx

一条评论您可以使用onClientClick并附加 JavaScript 函数。http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx

If you want to use jQuery, you can use ClientIDModeyou are able easier to figure out control IDs.

如果你想使用 jQuery,你可以使用ClientIDMode你能够更容易地找出控件 ID。

Take a look at http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx.

看看http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-和-net-4-0-series.aspx

回答by david holley

//Page_Validators is an array of validation controls in the page. 
if (Page_Validators != undefined && Page_Validators != null) 
{ 
    //Looping through the whole validation collection. 
    for(var i=0; i<Page_Validators.length; i++) 
    { 
        ValidatorEnable(Page_Validators[i]); 
        //if condition to check whether the validation was successfull or not. 
        if (!Page_Validators[i].isvalid) 
        { 
            break; 
        } 
    } 
}

//if condition to check whether the page was validated successfully. 
if(Page_IsValid) 
{ 
    alert('Success'); 
} 
else 
{ 
    alert('Failure'); 
}

Note the very last code example at the bottom of this page.

请注意本页底部的最后一个代码示例。