惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
罗磊的独立博客
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - Franky
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
博客园 - 叶小钗
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
博客园 - 【当耐特】
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
量子位
宝玉的分享
宝玉的分享
V
Visual Studio Blog
博客园_首页
IT之家
IT之家
V
V2EX
腾讯CDC
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
有赞技术团队
有赞技术团队
J
Java Code Geeks
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security

博客园 - JIN Weijie

基于Entity Framework的自定义分页,增删改的通用实现 基于Dapper的分页实现,支持筛选,排序,结果集总数,多表查询,非存储过程 让Windows 7变成WIFI热点 新浪微博RSS生成器Ver 1.0 同步Twitter帐号 修改Thickbox,预加载图片和点击图片前后浏览 ASTreeView 1.5.8发布(ASP.Net树控件) ASTreeView 1.5.3发布(ASP.NET树控件) ASTreeView 1.4.0发布(ASP.NET树控件) ASTreeView 1.3.0发布(ASP.NET树控件) ASTreeView 1.1.1发布(ASP.NET树控件) 在GoDaddy上部署SubText ASTreeView 1.0发布(一个ASP.NET树控件) 自定义增加Windows xp IIS的连接数 javascript closure(闭包)的一个示例 vmware增加磁盘空间方法以及出错解决 <=本博客已经转移至jinweijie.com=> [asp.net]优化ViewState [js]remove whitespace for firefox [windows]自动拨号脚本
[js][asp.net]客户端控制validator
JIN Weijie · 2008-12-24 · via 博客园 - JIN Weijie

Validators are a great part of the ASP.NET framework: they provide a standardized and easy way to add validation to form fields. But even if the framework provides different kinds of validators, there are so many different validations patterns that sometimes you have to write custom code to match your specific requirements.

I wrote a post a few months ago about how to write a custom validator for a Checkbox list, but I was dealing with a completely new validation pattern. But what if I only want to enable the validation of certain field based on certain conditions? Or if I want to validate based on an event that is not theOnChange event of the form field? And all of this while keeping the standard validation logic?

If the condition can be determined in the code-behind it's easy:

//Disable server side validation
myRequiredFieldValidator.Enabled = false;
//Disable client side validation
myRequiredFieldValidator.EnableClientScript = false;

But what if I want to enable the validator from javascript? I didn't find anything online about this so I debugged the ASP.NET js validation library to look for the method that enables the validation, and for the one that forces the validation check.

Enable a validator via javascript

The method to enable or disable a validator is:

function ValidatorEnable(val, enable)

where val is the reference to the <span> element that the validator uses to render the error message, and enable is a boolean to tell the method whether to enable or disable the validation.

Force a validation via javascript

In case you want to ask to a validator to do his job:

function ValidatorValidate(val, validationGroup, event)

val is again the reference to the validator <span>validationGroup is the name of the validation group of the element that is triggering the validation, event is the reference to the event that triggered the validation. But only the first parameter is useful when you want to force the validation, since the others are used only by the standard validation of ASP.NET.

A real life example

Let's see a example from the code I'm writing for one of the new features of Subtext. 
Imagine you have this snippet of HTML code:

<asp:DropDownList ID="ddlMimeType" runat="server">
<asp:ListItem Value="mp3">audio/mpeg</asp:ListItem>
<asp:ListItem Value="wma">audio/wma</asp:ListItem>
<asp:ListItem Value="other">Other</asp:ListItem>
</asp:DropDownList>
<asp:TextBox id="txbOtherMimetype" runat="server" />
<asp:RequiredFieldValidator id="valOtherMimetypeRequired" runat="server"
ControlToValidate="txbOtherMimetype" ForeColor="#990066"
ErrorMessage="You have to specify a custom mimetype." />

And you want to enable the valOtherMimeTypeRequired validator only when the option selected in the drop down is "other".

Using jQuery here is the javascript code to do the job:

<script type="text/javascript">
$(document).ready(function()
{
$("#<%= ddlMimeType.ClientID %>").change(function()
{
toggleOtherMimeType(this);
});
}

function toggleOtherMimeType(elem)
{
if(elem!=undefined)
{
if(elem.value=="other")
{
$("#<%= txbEnclosureOtherMimetype.ClientID %>").show();
ValidatorEnable($("#<%= valEncOtherMimetypeRequired.ClientID %>")[0], true);
}
else
{
$("#<%= txbEnclosureOtherMimetype.ClientID %>").hide();
ValidatorEnable($("#<%= valEncOtherMimetypeRequired.ClientID %>")[0], false);
}
}
}
</script>

Notice the <%= ddlMimeType.ClientID %> to select a server-side control using the automatically generated client ID and the $("#elemendId")[0] to get the real DOM element and not the jQuery wrapper around it.

And in case I wanted to force the validation I should have written:

ValidatorValidate($("#<%= valEncOtherMimeTypeRequired.ClientID %>")[0]);

It took me a while to find out this method name. I hope this will save you a bit of the time I spent hunting down the names using Firebug.