asp.net validate textbox - at least one text box must have data in(asp.net 验证文本框 - 至少一个文本框必须有数据)
问题描述
我有三个文本框,我想验证它们.至少一个文本框必须包含数据.
I have three textboxes and I want to validate them. At least one textbox must contain data.
我该怎么做?
(文本框是家庭电话号码,工作电话号码,手机号码,我需要检查至少指定一种联系方式)
(The textboxes are Home Phone No., Work Phone No., Mobile No. and I need to check at least one method of contact is specified)
推荐答案
使用自定义验证器,无需循环浏览页面上的文本框,因为这种方法可以获取页面上的所有文本框.ClientValidationFunction
中指定的 JavaScript 函数将为每个带有关联验证器的文本框调用.
Use a Custom Validator, there is no need to cycle through the text boxes on the page as this approach gets ALL of the text boxes on the page. The JavaScript function specified in the ClientValidationFunction
will be called for each text box with a validator associated with it.
<asp:TextBox ID="txtHomePhone" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvHomePhone" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="txtHomePhone" ValidateEmptyText="true"></asp:CustomValidator>
<asp:TextBox ID="txtWorkPhone" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvWorkPhone" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="txtWorkPhone" ValidateEmptyText="true"></asp:CustomValidator>
<asp:TextBox ID="txtMobilePhone" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvMobilePhone" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="txtMobilePhone" ValidateEmptyText="true"></asp:CustomValidator>
<script language="javascript">
function Validate(sender, args)
{
args.IsValid = false;
if(args.Value != "")
{
args.IsValid = true;
}
}
</script>
这篇关于asp.net 验证文本框 - 至少一个文本框必须有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:asp.net 验证文本框 - 至少一个文本框必须有数据


基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01