显示“通过”验证 asp.net (req. field)validator 后的图像或文本

发布于 2024-09-12 20:04:10 字数 406 浏览 1 评论 0原文

我有以下 asp.net 代码:

<asp:TextBox CssClass="mf" runat="server" ID="mail1" Width="270" />
<asp:RequiredFieldValidator ID="rfv1" 
                            runat="server" 
                            ControlToValidate="mail1" 
                            Display="Dynamic" />

现在,如果验证通过(无回发),我想显示图像(或文本)

jQuery 解决方案对我来说也很好,但如果 javascript 那就不太安全了已禁用。

I have the following asp.net code:

<asp:TextBox CssClass="mf" runat="server" ID="mail1" Width="270" />
<asp:RequiredFieldValidator ID="rfv1" 
                            runat="server" 
                            ControlToValidate="mail1" 
                            Display="Dynamic" />

Now I want to show an image (or text) if the validation passes on the fly (no postback)

A jQuery solution is also fine by me, but that wouldn't be so safe if javascript is disabled.

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

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

发布评论

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

评论(1

时常饿 2024-09-19 20:04:10

无需依赖外部框架,您就可以利用 ASP.Net 客户端验证框架来处理扩展/高级功能。通过这种方式,您可以使用框架,增强它而不是替换它。

只需一点点 JavaScript 就可以实现您想要的行为。

<asp:TextBox CssClass="mf" runat="server" ID="mail1" Width="270" OnChange="showValidationImage();" />
<asp:RequiredFieldValidator ID="rfv1" 
                        runat="server" 
                        ControlToValidate="mail1" ClientIDMode="Static"
                        Display="Dynamic" >
    <img src="../Image/fail.jpg" />
</asp:RequiredFieldValidator>
<img id="imgPass" src="../Image/pass.jpg" style="visibility:hidden" />
<script language="javascript">
    // This function will be called whenever the textbox changes
    // and effectively hide or show the image based on the state of
    // the client side validation.
    function showValidationImage() {
        if (typeof (Page_Validators) == "undefined") return;

        // isvalid is a property that is part of the ASP.Net
        // client side validation framework
        imgPass.style.visibility = rfv1.isvalid ? "visible" : "hidden";
    }
</script>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />

请注意,我使用的是 ASP.Net 4 的 ClientIDMode="Static" 功能,如果您不使用 .Net 4,那么您将需要使用标准 <%= rfv1.ClientID %> 服务器端包含以获取脚本中的客户端 ID。

Without relying on an external framework you can tap into the ASP.Net Client Side Validation framework to handle extended/advanced functionality. In this way you are working with the framework, augmenting it instead of replacing it.

A tiny bit of JavaScript is all that is needed to enable the behavior you want.

<asp:TextBox CssClass="mf" runat="server" ID="mail1" Width="270" OnChange="showValidationImage();" />
<asp:RequiredFieldValidator ID="rfv1" 
                        runat="server" 
                        ControlToValidate="mail1" ClientIDMode="Static"
                        Display="Dynamic" >
    <img src="../Image/fail.jpg" />
</asp:RequiredFieldValidator>
<img id="imgPass" src="../Image/pass.jpg" style="visibility:hidden" />
<script language="javascript">
    // This function will be called whenever the textbox changes
    // and effectively hide or show the image based on the state of
    // the client side validation.
    function showValidationImage() {
        if (typeof (Page_Validators) == "undefined") return;

        // isvalid is a property that is part of the ASP.Net
        // client side validation framework
        imgPass.style.visibility = rfv1.isvalid ? "visible" : "hidden";
    }
</script>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />

Notice here that I am using a feature of ASP.Net 4 with ClientIDMode="Static" if you are not using .Net 4 then you will need to use the standard <%= rfv1.ClientID %> server side include to get at the client id inside your script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文