ASP.NET表单验证 - 突出显示JS / DHTML领域 [英] ASP.NET form validation - highlight fields with JS/DHTML

查看:116
本文介绍了ASP.NET表单验证 - 突出显示JS / DHTML领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了一个经过今天上午缠着我的问题。我建立了有许多输入表单的ASP.NET Web表单应用程序,我想我的标准化如何管理验证。我想用内置的验证器(的RequiredFieldValidator,正则表达式等)。我的HTML要求是:

I have an issue that's been bugging me this morning. I'm building an ASP.NET webforms app that has many input forms and I'm trying to standardise how I manage validation. I would like to use the built-in validators (RequiredFieldValidator, Regex etc). My html requirements are:

验证之前:

<div class="formLine">
   <label for="fieldID">Form Label</label>
   <input type="text" id="fieldID" />
</div>

验证(在错误的情况下)后:

After validation (in case of error):

<div class="formLine formError">
<label for="fieldID">Form Label</label>
<input type="text" id="fieldID" />
<span class="errorMessage">Please enter some value</span>
</div>

额外的范围是罚款,这是与ASP.NET验证控件来实现的。我的问题是增加在 formError 类包含&LT; D​​IV&GT;

The additional span is fine, this is achieved with the ASP.NET validation controls. My problem is adding the formError class to the containing <div>.

我很舒服使用jQuery / DHTML来添加这个类,但就是不知道在哪里把它挂在是否有可能监测DOM使用jQuery的变化 - 例如火每当跨度添加为事件处理程序的子&LT; D​​IV CLASS =formLine&GT;

I'm comfortable using jQuery/DHTML to add this class, but just don't know where to hook it in. Is it possible to monitor DOM changes with jQuery - for example fire an event handler whenever a span is added as a child of <div class="formLine">?

别人也有处理这一什么好的建议?

Does anyone else have any good suggestions for dealing with this?

感谢您的阅读。

推荐答案

编辑:澄清之后

After clarification

您可以写由onClick的客户端事件调用自定义函数。由于您使用ASP.Net验证控件,你应该能够调用函数ValidatorValidate(VAL),其中val是验证器。然后,您可以检查校验SPAN标记的内容为您验证错误,并相应地更新您的div标签。

You could write a custom function that is called by the onClick client side event. Because you are using ASP.Net validation controls you should be able to call the function ValidatorValidate(val) where val is the validator. You could then check the contents of the validators SPAN tag for your validation error and update your div tag accordingly.

注:我在客户端验证没有专家,但这应该在主业

Note: I'm no expert on client side validation but this should work in principal.

MSDN上的进一步信息。


您需要声明的DIV的=服务器与ID标签,所以你可以在服务器端code引用它再在页面加载做Page.IsValid测试如果其中一个将返回false您验证失败vaildate。

You need to declare your div as runat="server" with an id tag so you can reference it in server side code then in page load do a test for Page.IsValid which will return false if one of your validators fails to vaildate.

您可以再编程设置你的div class属性。是这样的:

You can then set the class property on your div programatically. Something like:

<div class="formLine" id="divForm" runat="server">   
  <label for="fieldID">Form Label</label>   
  <input type="text" id="fieldID" />
</div>

code-背后(VB)

Code-Behind (VB)

If Not Page.IsValid Then
  divForm.Attributes.Remove("class")
  divForm.Attributes.Add("class", "formLine formError")
End If

这篇关于ASP.NET表单验证 - 突出显示JS / DHTML领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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