asp.net 验证文本框 - 至少一个文本框必须有数据

asp.net validate textbox - at least one text box must have data in(asp.net 验证文本框 - 至少一个文本框必须有数据)
本文介绍了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 验证文本框 - 至少一个文本框必须有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)