0

我的 aspx 中有 5 个文本框。当我点击保存按钮时,如何验证用户至少填写任何 3 个文本框?有可能这样做吗?谢谢

aspx:

<table width="100%">
<tr>
  <td>
     <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
  </td>
  <td>
     <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
  </td>
  <td>
     <asp:TextBox ID="txt3" runat="server"></asp:TextBox>
  </td>
 <td>
     <asp:TextBox ID="txt4" runat="server"></asp:TextBox>
  </td>
  <td>
     <asp:TextBox ID="txt5" runat="server"></asp:TextBox>
  </td>
</tr>
</table>

VB.net:

 Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

 End Sub
4

3 回答 3

2
<script type="text/javascript">

    function Required() {
        debugger;
        var i = 0;
        var get = $("input[type=text]");
        get.each(function () {
            if (this.value != "") {
                i = i + 1;
            }
        })
        if (i < 3) {
            EnableValidator("RequiredFieldValidator1");
        }
        else {
            DisableValidator("RequiredFieldValidator1");
        }
    }


    function EnableValidator(id) {
        if ($('#' + id)[0] != undefined) {
            ValidatorEnable($('#' + id)[0], true);
            $('#' + id).hide();
        }
    }

    //Code:: Validator Disabled ::
    function DisableValidator(id) {
        if ($('#' + id)[0] != undefined) {
            ValidatorEnable($('#' + id)[0], false);
        }
    }

</script>

只需放置一个验证器并执行此操作。它会起作用的。

于 2013-09-20T06:19:57.030 回答
1
public Int32 FindNoOfTextBox()
        {
            Int32 count=0;
            if (txt1.Text != "")
            {
                count++;
            }
            if (txt2.Text != "")
            {
                count++;
            }
            if (txt3.Text != "")
            {
                count++;
            }
            if (txt4.Text != "")
            {
                count++;
            }
            if (txt5.Text != "")
            {
                count++;
            }
            return count;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Int32 count=FindNoOfTextBox();
            if (count >= 3)
            {
               //when 3 or more than 3 textboxs contains values
            }
            else
            {
               //less than three textboxes contains values
            }

        }
于 2013-09-20T06:24:04.137 回答
0

您可以遍历文本框并计算有数据的数量。

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

    Dim numFilled As Integer = 0

    For index As Integer = 1 To 5
        If Not String.IsNullOrEmpty(CType(FindControl("txt" & index), TextBox).Text) Then
            numFilled += 1
        End If
    Next

    If numFilled >= 3 Then

    Else

    End If

End Sub
于 2013-09-20T13:24:31.297 回答