asp.net验证文本 - 至少一个文本框必须有数据 [英] asp.net validate textbox - at least one text box must have data in

查看:115
本文介绍了asp.net验证文本 - 至少一个文本框必须有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个文本框,我想验证它们。至少有一个文本框必须包含的数据。

I have three textboxes and I want to validate them. At least one textbox must contain data.

我怎样才能做到这一点?

How can I do this?

(文本框是家庭电话,工作电话号码,手机号码和我需要检查至少有一个指定的联系方法)

(The textboxes are Home Phone No., Work Phone No., Mobile No. and I need to check at least one method of contact is specified)

推荐答案

使用自定义验证器,没有必要以循环文本框的页面上,因为这种方法得到所有的文本框的页面上。中指定的JavaScript函数 ClientValidationFunction 将被要求与之相关的验证每个文本框。

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验证文本 - 至少一个文本框必须有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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