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 验证文本框 - 至少一个文本框必须有数据


- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 使用 rss + c# 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01