必填字段验证器不起作用 [英] Required field validator not working

查看:80
本文介绍了必填字段验证器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了必填字段验证器,然后使用了正则表达式验证器,但是必填字段验证器不起作用.....

I have used a required field validator followed by a regular expression validator but the required field validator is not working.....

<asp:TextBox ID="txtSummary" runat="server" TextMode="MultiLine" Width="700px"
    CssClass="txtStyle" Font-Names="Arial" MaxLength="1000" 
    ValidationGroup="Valtxt" TabIndex="2" Rows="4">
</asp:TextBox>

<asp:RegularExpressionValidator ID="regValSummary" runat="server"
    ControlToValidate="txtSummary" Display="Dynamic"
    ValidationExpression="[^&lt;&gt;&amp;#!]*" ValidationGroup="Valtxt">
        Invalid characters(&lt;&gt;&amp;#!) are not allowed
</asp:RegularExpressionValidator>

<asp:RequiredFieldValidator ID="reqvalSummary" runat="server"
    ControlToValidate="txtSummary" ErrorMessage="Summary is required"
    ValidationGroup="Valtxt" Display="Dynamic">
</asp:RequiredFieldValidator>

有人可以看到问题吗?

推荐答案

RequiredFieldValidator 由客户端 onchange 事件触发.听起来您好像期望它由 onblur 事件触发,这样,从文本框移开将触发验证.

The RequiredFieldValidator is triggered by the client side onchange event. It sounds like you're expecting it to be triggered by the onblur event, such that tabbing away from the textbox would fire the validation.

在转到此之前,我怀疑这是您所看到的,并且要验证它是否确实在工作,您需要触发 onchange .为此,请在文本框中输入一些文本,然后按Tab键,再次按Tab键,将其清除,清除文本框,然后再次按Tab键.现在,您应该看到RequiredFieldValidator的错误消息,因为它的内容已更改 .

Before jumping to that, I suspect this is what you are seeing and to validate that it's actually working you need to trigger onchange. To do so, enter some text in the textbox, tab away, tab back to it, clear the textbox, then tab away once more. You should now see the RequiredFieldValidator's error message since it's contents have changed.

回到 onblur 问题.要实现该行为,您可以在代码隐藏中添加 onblur 属性,并使其调用 ValidatorValidate(...) JavaScript方法,如下所示:

Back to the onblur issue. To accomplish that behavior you could add the onblur attribute in your code-behind and have it call the ValidatorValidate(...) JavaScript method as follows:

void Page_Load(object sender, EventArgs e)
{
    txtSummary.Attributes.Add("onblur", "ValidatorValidate(" + reqvalSummary.ClientID + ")");
}

或者,您可以在标记中完成相同的操作.首先,添加此脚本块:

Alternately, you could accomplish the same thing in markup. First, add this script block:

<script type="text/javascript">
    function rfvBlur() {
        var rfv = document.getElementById("<%= reqvalSummary.ClientID %>");
        ValidatorValidate(rfv);
    }    
</script>

第二,通过添加 onblur ="rfvBlur()" 来更新< asp:TextBox .../> 标记,以使它现在看起来像这样:

Second, update the <asp:TextBox.../> markup by adding onblur="rfvBlur()" so that it now looks like this:

<asp:TextBox ID="txtSummary" runat="server" TextMode="MultiLine" Width="700px" CausesValidation="true"
            CssClass="txtStyle" Font-Names="Arial" MaxLength="1000" ValidationGroup="Valtxt"
            TabIndex="2" Rows="4" onblur="rfvBlur()" />

另一种选择是通过将以下属性添加到您的< asp:TextBox .../> 标记(不需要其他脚本块)来验证整个ValidationGroup:

Yet another option is to validate the entire ValidationGroup by adding the following attribute to your <asp:TextBox.../> markup (no additional script block needed):

onblur="Page_ClientValidate('Valtxt')"

这篇关于必填字段验证器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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