我怎样才能关闭ASP.NET必填字段验证器QUOT;失去焦点"行为 [英] How can i turn off ASP.NET required field validator "lost focus" behaviour

查看:207
本文介绍了我怎样才能关闭ASP.NET必填字段验证器QUOT;失去焦点"行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code,我需要两个独立的必填字段验证为一个控制,无论是在独立的验证组,然后由两个独立的按钮确认。

I have some code where I need two separate required field validators for one control, both in separate validation groups which are then validated by two separate buttons.

此方法效果很好时,按下按钮但两者的验证表明,如果我在文本框中输入一个值,然后将其删除。

This approach works well when the buttons are clicked but both validators show if I enter a value in the textbox and then remove it.

有没有办法把这个失去焦点确认了吗?我只需要在单击按钮时它来验证。

Is there a way to turn this"lost focus" validation off? I only need it to validate when the buttons are clicked.

修改

不幸的是,如果我设置EnableClientScript = false,那么我没有的任何的客户端通知。我想是动态的错误消息显示(用途不同的按钮的OnClientClick事件),但不是失去焦点文本框。

Unfortunately, if I set EnableClientScript=false then I dont have any client notifications. What I want is for the dynamic error message to show (effectivly in the OnClientClick event of the button) but not the "lost focus" of the textbox.

有一些方法我可以禁用或脱钩的引发LostFocus客户端事件?

Is there some way I can disable or "unhook" the lostfocus client event?

修改

组合dDejan的回答和womp的ANSWEER <一个href=\"http://stackoverflow.com/questions/2158052/enable-disable-asp-net-validator-controls-within-a-specific-validationgroup-wit\">here完美排序的问题。

A combination dDejan's answer and womp's answeer here sorted the problem perfectly.

我的最后code看起来像这样(为别人有类似的情况)...

My final code looks like this (for anyone else with a similar situation)...

...的Javascript

Javascript...

<script type="text/javascript">

    $(document).ready(function() {
        $('body').fadeIn(500);

        //Turn off all validation = its switched on dynamically
        $.each(Page_Validators, function(index, validator) {
                ValidatorEnable(validator, false);
        });
    });

    function ToggleValidators(GroupName) {

        $.each(Page_Validators, function(index, validator) {
            if (validator.validationGroup == GroupName) {

                ValidatorEnable(validator, true);

            } else {
                ValidatorEnable(validator, false);
            }
        });
    }

</script>

ASPX控制示例...

ASPX Control Example...

<telerik:RadTextBox Width="196px" ID="txtFirstName" runat="server" MaxLength="50" Skin="Black"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="valFirstName" CssClass="Validator" runat="server" EnableClientScript="true" Display="Dynamic" ErrorMessage="You  must enter your first name." ControlToValidate="txtFirstName" ValidationGroup="NeededForEmail"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" CssClass="Validator" runat="server" EnableClientScript="true" Display="Dynamic" ErrorMessage="You  must enter your first name." ControlToValidate="txtFirstName" ValidationGroup="NeededForSubmit"></asp:RequiredFieldValidator>

ASPX按钮code ...

ASPX Button Code...

<asp:Button ID="btnGetConfCode" runat="server" Text="Get Confirmation Code" OnClientClick="ToggleValidators('NeededForEmail')" OnClick="btnGetConfCode_Click" Width="100%" ValidationGroup="NeededForEmail"/>

<asp:Button ID="btnRegisterUser" runat="server" Text="Register" OnClientClick="ToggleValidators('NeededForSubmit')" OnClick="btnRegisterUser_Click" Width="100px" ValidationGroup="NeededForSubmit" />

所以,现在有没有验证,直到用户点击或者获取电子邮件确认code按钮或注册按钮。

So, now there is no validation until a user clicks either the "Get Email Confirmation Code" button or the "Register" button.

如果他们点击获取电子邮件确认code按钮,所有的控件从用户的输入电子邮件验证code中的文本框验证分开,我们只看到一个校验信息。

If they click the "Get Email Confirmation Code" button all of the controls validate apart from the textbox where the user is to input the email validation code and we only see one validator message.

如果他们点击注册按钮,然后所有的控制验证,我们只能看到一次确认的消息。

If they click the "Register" Button then all of the controls validate and we only see one validation message.

如果任一按钮为pressed,用户可以追溯到,添加,然后删除一些文本,然后我们只看到一个校验。这种变化之前,你见惯了这两个消息说同样的事情。

If either button is pressed, the user goes back, adds and then removes some text then we only see one validator. Before this change you used to see both messages saying the same thing.

感谢您的帮助球员

推荐答案

可以,如果验证程序是使用主动或不符合客户端code设置 ValidatorEnable 功能。基本上,它是这样的。

You can set if the validators are "active" or not with client side code using the ValidatorEnable function. Basically it goes like this

var validator = document.getElementById('<%=Validator1.ClientID%>');
ValidatorEnable(validator , state); //where state is boolean

您还可以触发验证程序来验证一些事件中使用 ValidatorValidate(验证)(例如像按钮的点击)功能。

You can also trigger the validator to validate on some event (like for example the click of the buttons) using the ValidatorValidate(validator) function.

我不知道这会更好地为您(启用/禁用触发验证的验证或自定义),但我认为这篇文章,将引导你在正确的方向

I am not sure which would work better for you (enabling/disabling the validators or custom triggering of the validation) but I suggest this article that will guide you in the right direction

ASP.NET验证在深度

这篇关于我怎样才能关闭ASP.NET必填字段验证器QUOT;失去焦点&QUOT;行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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