通过 javascript 调用所需的字段验证器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6415091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invoke required field validator through javascript
提问by Maxim Gershkovich
I have a non standard submit button in my ASP.NET form along the lines of.
我的 ASP.NET 表单中有一个非标准提交按钮。
<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?
因此,我的必填字段验证器没有在客户端被调用。如何通过 Javascript 调用所需的字段验证器?
Alternatively is there a better way to accomplish what I am attempting to do?
或者有没有更好的方法来完成我正在尝试做的事情?
回答by santosh singh
You can use in built client side function named Page_ClientValidate
for this.Check out the following code snippet for the reference
您可以在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>
回答by Martin Belcher - AtWrk
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)
Page_ClientValidate
触发表单上所有验证器的验证,正如@gilly3 所示,您还可以通过循环集合和调用来验证它们 ValidatorValidate(validator)
However if you want to validate just one particular validator then you need to call ValidatorValidate(validator)
for just one item.
但是,如果您只想验证一个特定的验证器,那么您只需要调用ValidatorValidate(validator)
一项。
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.
验证器参数需要是一个 DOM 对象,这可能很难获取,因为如果您使用母版页或用户控件,元素 ID 最终可能与您在标记中指定的完全不同。
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
我在我的一个项目中通过使用这样的 jQuery 选择器解决了这个问题
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.
ASP.NET 只为 ID 添加前缀以创建唯一名称我可以使用id$=
选择器的一部分来匹配任何以"rfvCampaignFile"
我编写网站后结尾的 ID我知道它不会与其他控件发生冲突。最后,我使用.get(0)
将 DOM 对象引用返回到第一个(并且仅在我的情况下)匹配的 DOM 对象。
回答by gilly3
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:
查看ASP.Net 验证器的客户端 api。页面上的所有验证器都在Array
via中公开Page_Validators
。您可以调用ValidatorValidate(validator)
单个验证器来调用它。因此,要调用页面上的所有验证器,您可以:
Page_Validators.forEach(ValidatorValidate);
By the way, to use Array.forEach
in older browsers you'll need to extend Array.
顺便说一句,要Array.forEach
在旧浏览器中使用,您需要扩展 Array。
回答by Warren LaFrance
If you are using jquery then set a ASP.NET control ClientIDMode = "Static"
and you can make your code easier to read as:
如果您使用的是 jquery,则设置一个 ASP.NET 控件ClientIDMode = "Static"
,您可以使您的代码更易于阅读:
ValidatorValidate($('#MyControl'));
or something very similar.
或非常相似的东西。