通过 javascript 调用必填字段验证器
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为此,您可以使用名为
Page_ClientValidate
的内置客户端函数。查看以下代码片段以供参考You can use in built client side function named
Page_ClientValidate
for this.Check out the following code snippet for the referencePage_ClientValidate
触发表单上所有验证器的验证,正如 @gilly3 所示,您还可以通过循环集合并调用ValidatorValidate(validator)
来验证所有验证器,但是如果您想仅验证一个特定的验证器,那么您只需为一项调用 ValidatorValidate(validator)。
验证器参数需要是一个 DOM 对象,该对象可能很难获取,因为如果您使用母版页或用户控件,则元素 ID 最终可能与您在标记中指定的完全不同。
例如
,
我在我的一个项目中通过使用像这样的 jQuery 选择器解决了这个问题
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 callingValidatorValidate(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.
becomes
I got around this in one of my projects by using a jQuery selector like this
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 验证器的客户端 API 。页面上的所有验证器都通过
Page_Validators
在Array
中公开。您可以在单个验证器上调用 ValidatorValidate(validator) 来调用它。因此,要调用页面上的所有验证器,您可以:顺便说一下,在旧版浏览器中使用 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
viaPage_Validators
. You can callValidatorValidate(validator)
on a single validator to invoke it. So, to invoke all validators on the page you could:By the way, to use
Array.forEach
in older browsers you'll need to extend Array.如果您使用 jquery,则设置 ASP.NET 控件
ClientIDMode = "Static"
,这样您就可以使代码更易于阅读:或非常类似的内容。
If you are using jquery then set a ASP.NET control
ClientIDMode = "Static"
and you can make your code easier to read as:or something very similar.