通过 javascript 调用必填字段验证器

发布于 2024-11-16 13:40:47 字数 259 浏览 3 评论 0原文

我的 ASP.NET 表单中有一个非标准提交按钮,类似于。

<a class="button" href="#" onclick="this.blur();SubmitForm();"><span>Submit</span></a>

因此,我的必填字段验证器不会在客户端被调用。如何通过Javascript调用必填字段验证器?

或者有更好的方法来完成我正在尝试做的事情吗?

I have a non standard submit button in my ASP.NET form along the lines of.

<a class="button" href="#" onclick="this.blur();SubmitForm();"><span>Submit</span></a>

Due to this, my required field validator is not being invoked on the client side. How can the required field validator be invoked through Javascript?

Alternatively is there a better way to accomplish what I am attempting to do?

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

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

发布评论

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

评论(4

一片旧的回忆 2024-11-23 13:40:47

为此,您可以使用名为 Page_ClientValidate 的内置客户端函数。查看以下代码片段以供参考

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientSide_Validation.aspx.cs"
    Inherits="ClientSide_Validation" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function performCheck() {

            if (Page_ClientValidate()) {
            }

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lbl1" runat="server" Text="Please enter some value"></asp:Label>
        <asp:TextBox ID="txtbox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="valReq" ControlToValidate="txtbox1" runat="server"
            ErrorMessage="*" Display="Dynamic">
        </asp:RequiredFieldValidator>
        <input type="button" id="btnValidate" value="Submit" onclick="performCheck()" />
        <a href="#" onclick="performCheck();">Validate</a>
    </div>
    </form>
</body>
</html>

You can use in built client side function named Page_ClientValidate for this.Check out the following code snippet for the reference

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientSide_Validation.aspx.cs"
    Inherits="ClientSide_Validation" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function performCheck() {

            if (Page_ClientValidate()) {
            }

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lbl1" runat="server" Text="Please enter some value"></asp:Label>
        <asp:TextBox ID="txtbox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="valReq" ControlToValidate="txtbox1" runat="server"
            ErrorMessage="*" Display="Dynamic">
        </asp:RequiredFieldValidator>
        <input type="button" id="btnValidate" value="Submit" onclick="performCheck()" />
        <a href="#" onclick="performCheck();">Validate</a>
    </div>
    </form>
</body>
</html>
〃安静 2024-11-23 13:40:47

Page_ClientValidate 触发表单上所有验证器的验证,正如 @gilly3 所示,您还可以通过循环集合并调用 ValidatorValidate(validator) 来验证所有验证器,

但是如果您想仅验证一个特定的验证器,那么您只需为一项调用 ValidatorValidate(validator)。

验证器参数需要是一个 DOM 对象,该对象可能很难获取,因为如果您使用母版页或用户控件,则元素 ID 最终可能与您在标记中指定的完全不同。

例如

<asp:RequiredFieldValidator ID="rfvCampaignStartDate" runat="server" .../>

<span id="cph_0_rfvCampaignFile" ...>

我在我的一个项目中通过使用像这样的 jQuery 选择器解决了这个问题

ValidatorValidate($('[id$="rfvCampaignFile"]').get(0));

ASP.NET 只为 ID 添加前缀来创建一个唯一的名称,我可以使用选择器的 id$= 部分来匹配任何ID 以 "rfvCampaignFile" 结尾,因为我编写了该网站,所以我知道它不会与其他控件发生冲突。最后,我使用 .get(0) 将 DOM 对象引用返回到第一个(仅在我的情况下)匹配的 DOM 对象。

Page_ClientValidate triggers validation for all validators on the form and as @gilly3 shows out you can also validate them all by looping the collection and calling ValidatorValidate(validator)

However if you want to validate just one particular validator then you need to call ValidatorValidate(validator) for just one item.

The validator argument needs to be a DOM object which can be tricky to get because the element ID might end up quite different than you specified in the mark up if you are using master pages or user controls.

e.g.

<asp:RequiredFieldValidator ID="rfvCampaignStartDate" runat="server" .../>

becomes

<span id="cph_0_rfvCampaignFile" ...>

I got around this in one of my projects by using a jQuery selector like this

ValidatorValidate($('[id$="rfvCampaignFile"]').get(0));

ASP.NET only prefixes the IDs to create a unique name I could use id$= part of the selector to match any IDs ending in "rfvCampaignFile" since I wrote the website I know it won't clash with other controls. Finally I use .get(0) to return the DOM object reference to the first (and only in my case) DOM object matched.

情释 2024-11-23 13:40:47

查看ASP.Net 验证器的客户端 API 。页面上的所有验证器都通过 Page_ValidatorsArray 中公开。您可以在单个验证器上调用 ValidatorValidate(validator) 来调用它。因此,要调用页面上的所有验证器,您可以:

Page_Validators.forEach(ValidatorValidate);

顺便说一下,在旧版浏览器中使用 Array.forEach 您需要扩展 Array

Have a look at the client side api for ASP.Net validators. All the validators on the page are exposed in an Array via Page_Validators. You can call ValidatorValidate(validator) on a single validator to invoke it. So, to invoke all validators on the page you could:

Page_Validators.forEach(ValidatorValidate);

By the way, to use Array.forEach in older browsers you'll need to extend Array.

墨落画卷 2024-11-23 13:40:47

如果您使用 jquery,则设置 ASP.NET 控件 ClientIDMode = "Static",这样您就可以使代码更易于阅读:

ValidatorValidate($('#MyControl'));

或非常类似的内容。

If you are using jquery then set a ASP.NET control ClientIDMode = "Static" and you can make your code easier to read as:

ValidatorValidate($('#MyControl'));

or something very similar.

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