asp.net 中的字段验证器
是否可以使用“必填字段验证器”控件来验证多个字段(例如,我有 12 个必需的文本框。我想尝试避免使用 12 个 RFV 控件。如果确实触发了验证,有没有办法显示自定义消息(“textA 为空”或“textB 为空”)等?
Is it possible to have "Required Field Validator" controls to validate more than one field (example I have 12 textboxes that are required. I want to try an avoid having 12 RFV controls. If a validation does get triggered, is there a way to display a customized message ("textA is empty" or "textB is empty") etc.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以创建一个自定义验证器来验证所有控件。
http://msdn.microsoft.com/ en-us/library/system.web.ui.webcontrols.customvalidator.aspx
必填字段验证器一次只能验证一个控件。
You can create a custom validator that goes through validates all the controls.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
The Required Field Validator can only validate one control at a time.
您不能使用
RequiredFieldValidator
来做到这一点;您可以编写自己的CustomValidator
来执行此操作,但验证将在服务器端而不是客户端进行。You cannot do that with a
RequiredFieldValidator
; you could write your ownCustomValidator
to do that, but the validation would be on the server side rather than on the client side.多字段验证器 - ASP.NET 验证控件正是您所需要的。
Multiple Fields Validator - An ASP.NET Validation Control is what you need.
正如其他人所提到的,您可以创建可以在客户端和服务器端进行验证的 CustomValidator。您必须牢记以下几点。
1) 确保将您的客户端脚本公开为 Web 资源。这将使脚本能够被浏览器缓存。
2) 使用特定的属性来定位特定的文本框。这可以通过给它们一个特定的类来执行,该类将在您的自定义验证器控件中进行验证。
希望有帮助!
As mentioned by everyone else you can create your CustomValidator that can validate on the client side and on the server side. There are couple of things that you must keep in mind.
1) Make sure to expose your client script as a Web Resource. This will enable the script to be cached by the browser.
2) Use a certain attribute to target certain TextBoxes. This can be performed by giving them a certain class which will be validated in your Custom Validator control.
Hope it helps!