对于ASP网客户端验证,验证事件 [英] Validation event for asp net client side validation

查看:132
本文介绍了对于ASP网客户端验证,验证事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有来装一个自定义函数ASP.NET的客户端验证活动的一种方式,所以每次验证由任何控制燃煤我可以对客户端UI

一些魔法

我在寻找一个通用的方法没有设置它在每一个导致回发控制拦截页面onvalidating事件

感谢你们

编辑:

我结束了此功能:(感谢@Kirk)

  $(文件)。就绪(函数(){
    $('形式')。提交(函数(){
        如果(typeof运算Page_Validators!='不确定'){
            VAR误差='';
            $。每个(Page_Validators,函数(){
                如果(!this.isvalid){
                    错误+ = this.errormessage +'\\ r \\ n';
                }
            });
            如果(errors.length大于0){
                警报(错误);
            }
        }
    });
});


解决方案

要做到沿此线的东西你可以提交按钮或只是一般的表单提交事件放置的OnClientClick事件。

然后就可以与验证控件使用客户端验证对象模型。这实际上可以验证每个验证控制你的设置。有一对夫妇的值可以从相关的网页客户端对证,请参阅http://msdn.microsoft.com/en-us/library/yb52a4x0.aspx#ClientSideValidation_ClientValidationObjectModel.

您参考与的isValid 属性每个控件。例如:

 < ASP:标签ID =lblZip=服务器文本=邮编code:/>
< ASP:文本框的id =txtZip=服务器/>< / ASP:文本框>
< ASP:RegularEx pressionValidator ID =valZip=服务器
   的ControlToValidate =txtZip
   的ErrorMessage =无效邮编code
   ValidationEx pression =[0-9] {5}/>< SCRIPT LANGUAGE = JavaScript的>
//调用此函数做一些事情
功能txtZipOnChange(){
   //什么都不做,如果客户端验证未激活
   如果(typeof运算(Page_Validators)==未定义)返回;
       //更改标签的颜色
       lblZip.style.color = valZip.isvalid? 黑:红;
}
< / SCRIPT>

您也可以得到验证的页面上的阵列与客户端功能 Page_Validators 。还有,你可以使用一些更多的功能。

您也可以使用 ValidatorValidate(VAL)客户端功能,迫使每一个检查indivually以及 ValidatorEnable(VAL,使能) 来启用或禁用验证器为你的逻辑要求。

看看 http://msdn.microsoft.com/ EN-US /库/ aa479045.aspx#aspplusvalid_clientside 了解一些详细信息。

希望这可以让你在哪里,你需要去。如果没有,随意问。

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

如果你想使用jQuery,您可以使用的ClientIDMode 你能更容易弄清楚控件ID。

看看<一个href=\"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\" rel=\"nofollow\">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.

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

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

Thank you guys

Edit:

I ended up with this function: (thanks to @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);
            }
        }
    });    
}); 

解决方案

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.

Then you can use the Client Validation Object Model with 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.

You reference each control with isvalid property. For example

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

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.

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

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

Previous Comment You can use an onClientClick and attach a JavaScript function. http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx

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

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.

这篇关于对于ASP网客户端验证,验证事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆