需要两个文本框,一个或两个

时间:2022-01-19 17:20:09

I have two textboxes on an asp.net webpage, either one or both are required to be filled in. Both cannot be left blank. How do I create a validator to do this in asp.net?

我在asp.net网页上有两个文本框,其中一个或两个都需要填写。两者都不能留空。如何在asp.net中创建验证器?

4 个解决方案

#1


You'd need a CustomValidator to accomplish that.

您需要一个CustomValidator才能实现这一目标。

Here is some code demonstrating basic usage. The custom validator text will show after IsValid is called in the submit callback and some text will be displayed from the Response.Write call.

以下是一些演示基本用法的代码。在提交回调中调用IsValid之后将显示自定义验证器文本,并且将从Response.Write调用中显示一些文本。

ASPX

    <asp:TextBox runat="server" ID="tb1" />
    <asp:TextBox runat="server" ID="tb2" />

    <asp:CustomValidator id="CustomValidator1" runat="server" 
      OnServerValidate="TextValidate" 
      Display="Dynamic"
      ErrorMessage="One of the text boxes must have valid input.">
    </asp:CustomValidator>

    <asp:Button runat="server" ID="uxSubmit" Text="Submit" />

Code Behind

    protected void Page_Load(object sender, EventArgs e)
    {
        uxSubmit.Click += new EventHandler(uxSubmit_Click);
    }

    void uxSubmit_Click(object sender, EventArgs e)
    {
        Response.Write("Page is " + (Page.IsValid ? "" : "NOT ") + "Valid");
    }

    protected void TextValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = (tb1.Text.Length > 0 || tb2.Text.Length > 0);
    }

#2


Try a CustomValidator.

尝试CustomValidator。

You'll need to create a method that does the following to handle the ServerValidate event:

您需要创建一个方法来执行以下操作来处理ServerValidate事件:

void ServerValidation (object source, ServerValidateEventArgs args)
 {
    args.IsValid = TextBox1.Text.Length > 0 || TextBox2.Text.Length > 0;
 }

#3


In addition to creating server-side validation, you can use the ClientValidationFunction property on the CustomValidator to provide client-side validation as well. That might look something like this:

除了创建服务器端验证之外,您还可以使用CustomValidator上的ClientValidationFunction属性来提供客户端验证。这可能看起来像这样:

function(sender, args) {
    args.IsValid = document.getElementById('<%=TextBox1.ClientID%>').value != '' 
                   || document.getElementById('<%=TextBox2.ClientID%>').value != '';
}

#4


Onclientclick of your button or whatever submits your page call a javascript function like this

Onclientclick您的按钮或任何提交您的页面调用这样的JavaScript函数

function valtxtbox(){


if (document.getElementById('<%=TextBox1.ClientID%>').value== '' && document.getElementById('<%=TextBox2.ClientID%>').value== '')
{

alert('You must enter in data!');
return false;
}

#1


You'd need a CustomValidator to accomplish that.

您需要一个CustomValidator才能实现这一目标。

Here is some code demonstrating basic usage. The custom validator text will show after IsValid is called in the submit callback and some text will be displayed from the Response.Write call.

以下是一些演示基本用法的代码。在提交回调中调用IsValid之后将显示自定义验证器文本,并且将从Response.Write调用中显示一些文本。

ASPX

    <asp:TextBox runat="server" ID="tb1" />
    <asp:TextBox runat="server" ID="tb2" />

    <asp:CustomValidator id="CustomValidator1" runat="server" 
      OnServerValidate="TextValidate" 
      Display="Dynamic"
      ErrorMessage="One of the text boxes must have valid input.">
    </asp:CustomValidator>

    <asp:Button runat="server" ID="uxSubmit" Text="Submit" />

Code Behind

    protected void Page_Load(object sender, EventArgs e)
    {
        uxSubmit.Click += new EventHandler(uxSubmit_Click);
    }

    void uxSubmit_Click(object sender, EventArgs e)
    {
        Response.Write("Page is " + (Page.IsValid ? "" : "NOT ") + "Valid");
    }

    protected void TextValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = (tb1.Text.Length > 0 || tb2.Text.Length > 0);
    }

#2


Try a CustomValidator.

尝试CustomValidator。

You'll need to create a method that does the following to handle the ServerValidate event:

您需要创建一个方法来执行以下操作来处理ServerValidate事件:

void ServerValidation (object source, ServerValidateEventArgs args)
 {
    args.IsValid = TextBox1.Text.Length > 0 || TextBox2.Text.Length > 0;
 }

#3


In addition to creating server-side validation, you can use the ClientValidationFunction property on the CustomValidator to provide client-side validation as well. That might look something like this:

除了创建服务器端验证之外,您还可以使用CustomValidator上的ClientValidationFunction属性来提供客户端验证。这可能看起来像这样:

function(sender, args) {
    args.IsValid = document.getElementById('<%=TextBox1.ClientID%>').value != '' 
                   || document.getElementById('<%=TextBox2.ClientID%>').value != '';
}

#4


Onclientclick of your button or whatever submits your page call a javascript function like this

Onclientclick您的按钮或任何提交您的页面调用这样的JavaScript函数

function valtxtbox(){


if (document.getElementById('<%=TextBox1.ClientID%>').value== '' && document.getElementById('<%=TextBox2.ClientID%>').value== '')
{

alert('You must enter in data!');
return false;
}