必填字段验证器不起作用

发布于 2024-09-30 06:22:37 字数 862 浏览 1 评论 0原文

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

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

任何人都可以看到问题吗???

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="[^<>&#!]*" ValidationGroup="Valtxt">
        Invalid characters(<>&#!) are not allowed
</asp:RegularExpressionValidator>

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

can anyone sees the problem???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

柏拉图鍀咏恒 2024-10-07 06:22:37

RequiredFieldValidator 由客户端 onchange 事件触发。听起来您希望它由 onblur 事件触发,这样从文本框移开就会触发验证。

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

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

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

或者,您可以完成标记中同样的事情。首先,添加此脚本块:

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

其次,通过添加 onblur="rfvBlur()" 来更新 标记,以便现在看起来像这样:

<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()" />

另一种选择是通过将以下属性添加到 标记来验证整个 ValidationGroup(不需要额外的脚本块):

onblur="Page_ClientValidate('Valtxt')"

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.

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.

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>

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()" />

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')"
谎言月老 2024-10-07 06:22:37

将此行添加到 web.config 部分对我有用(当项目升级到 .NET 4.5 时所有验证器停止工作时,我遇到了问题):

<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />

来源:

http://forums.asp.net/t/1876231.aspx?ASP+Net+4+5+Validation+Controls+not+working+with+AJAX+ToolkitScriptManager1

Adding this line to <appSettings> section of web.config worked for me (I had a problem when all validators stopped working when project was upgraded to .NET 4.5):

<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />

Source:

http://forums.asp.net/t/1876231.aspx?ASP+Net+4+5+Validation+Controls+not+working+with+AJAX+ToolkitScriptManager1

温柔一刀 2024-10-07 06:22:37

为什么不更改“RegEx”验证器的正则表达式来检查文本框是否为空,而不是使用另一个验证器?

无论如何,您可能没有为引发回发的按钮或控件指定 ValidationGroup="Valtxt"。只需将 ValidationGroup="Valtxt" 添加到将帖子提升到页面的按钮或服务器控件

Why don't you change the regular expression of the "RegEx" validator to check if the textbox is empty instead of use another validator?

Anyway probabily you have not specify ValidationGroup="Valtxt" for the button or the control that raise the postback. Just add ValidationGroup="Valtxt" to the button or the server control that raise the post to the page

避讳 2024-10-07 06:22:37

我将以下内容放在 btn_Click 事件处理程序的顶部(以防止进一步执行代码),并且在“返回”时,rfv 消息显示...

        Page.Validate("your validation group");
        if (!Page.IsValid)
        {
            return;
        }

I put the following at the top of my btn_Click event handler (to prevent further code execution) and upon 'return', the rfv messages show...

        Page.Validate("your validation group");
        if (!Page.IsValid)
        {
            return;
        }
苹果你个爱泡泡 2024-10-07 06:22:37
<asp:TextBox ID="txtSummary" runat="server" TextMode="MultiLine" Width="700px"
    CssClass="txtStyle" Font-Names="Arial" MaxLength="1000" 
    TabIndex="2" Rows="4">
</asp:TextBox>

<asp:RegularExpressionValidator ID="regValSummary" runat="server"
    ControlToValidate="txtSummary" ErrorMessage="Invalid characters(<>&#!) are not allowed" Text="*"
    ValidationExpression="[^<>&#!]*" ValidationGroup="Valtxt">

</asp:RegularExpressionValidator>

<asp:RequiredFieldValidator ID="reqvalSummary" runat="server"
    ControlToValidate="txtSummary" ErrorMessage="Summary is required" Text="*"
    ValidationGroup="Valtxt">
</asp:RequiredFieldValidator>
<asp:TextBox ID="txtSummary" runat="server" TextMode="MultiLine" Width="700px"
    CssClass="txtStyle" Font-Names="Arial" MaxLength="1000" 
    TabIndex="2" Rows="4">
</asp:TextBox>

<asp:RegularExpressionValidator ID="regValSummary" runat="server"
    ControlToValidate="txtSummary" ErrorMessage="Invalid characters(<>&#!) are not allowed" Text="*"
    ValidationExpression="[^<>&#!]*" ValidationGroup="Valtxt">

</asp:RegularExpressionValidator>

<asp:RequiredFieldValidator ID="reqvalSummary" runat="server"
    ControlToValidate="txtSummary" ErrorMessage="Summary is required" Text="*"
    ValidationGroup="Valtxt">
</asp:RequiredFieldValidator>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文