优雅的方式来ASP.NET验证与jQuery结合 [英] Elegant way to combine ASP.NET validation with JQuery

查看:109
本文介绍了优雅的方式来ASP.NET验证与jQuery结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能最好的ASP.NET客户端验证模型相结合的JQuery?

How can I best combine JQuery with ASP.NET client side validation model?

我通常避免实施ASP.NET验证模型,因为它总是矫枉过正似乎是我在做什么。对于网站,我现在正在我只是收集非关键用户数据,只需要稍微基本验证。我不想出现在DOM或任何类似的消息。我总是发现很难得到这看起来正确反正。

I've generally avoided implementing ASP.NET validation model because it always seems overkill for what I was doing. For the site I'm working on now I'm just collecting non critical user data and only need somewhat basic validation. I dont want messages appearing in the DOM or anything like that. I've always found it hard to get that to look right anyway.

不过,我现在需要多一点优雅的实现了某些东西。我想利用在JQuery的优点是聪明的搜索前pressions像'告诉我,如果这些复选框中的至少一个被选中。我是新来的JQuery,但我认为这是关于1号线的jQuery和很多传统的ASP.NET模型更加复杂。

But I need now to implement something a little more elegant. What I want to take advantage of in JQuery is clever search expressions like 'tell me if at least one of these checkboxes is checked'. I'm new to JQuery, but I think this is about 1 line of JQuery and a lot more complicated in the traditional ASP.NET model.

所以我想利用jQuery的能力充分利用,但不能完全undemine ASP.NET的验证模式。

So I want to take full advantage of JQuery's abilities but not completely undemine ASP.NET's validation model.

我的最好的办法,到目前为止是这样的(其中pretty多少是ASP.NET的背后):

My best approach so far is this (which pretty much goes behind the back of ASP.NET):

$('#<%=btnJoinMailingList.ClientID %>').bind('click', function(event) {

   if (...) {
       alert("You must enter a name");
       return false;
   }     
   return true;
});

什么是更好的方法吗?是否有任何建议的插件jQuery的?

What's a better approach here? Are there any recommended plugins for JQuery ?

PS。我不希望使用MVC模式。我试图创建一个非常RAD网站,并没有时间去钻研好玩的新东西呢。

PS. I don't want to use MVC model. I'm trying to create a very 'RAD' site and dont have time to delve into that fun new stuff yet.

推荐答案

ASP.NET有许多验证控件,其中有一个是的 的CustomValidator 。结果
你可以给它的JavaScript函数使用jQuery和回报真正通过参数定制。您可以使用此控件自动显示错误信息,或者只是运行了jQuery code和手动处理的显示。

ASP.NET has many validation controls, one of them is the CustomValidator.
You can give it a custom JavaScript function that uses jQuery and returns true or false via argument. You can use this control to display the error message automatically, or just run the jQuery code and handle the display manually.

.aspx的:

Aspx:

<asp:CustomValidator ID="CustomValidator1" runat="server" Display="None"
                     ClientValidationFunction="checkTextareaLengths">
</asp:CustomValidator>

JavaScript的:

JavaScript:

function checkTextareaLengths(source, args){
  args.IsValid = true;
  $('textarea').each(function(){
    if($(this).text().lenght > 400)
      args.IsValid = false;
  });
}

这篇关于优雅的方式来ASP.NET验证与jQuery结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆