簡體   English   中英

檢查aspx中空文本框的總數

[英]Check the total of empty textbox in aspx

我的aspx中有5個文本框。 當我單擊“保存”按鈕時,如何驗證用戶至少填寫任何三個文本框? 有可能這樣做嗎? 謝謝

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
<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>

只需放置一個驗證器並執行此操作即可。 它會工作。

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
            }

        }

您可以遍歷文本框並計算包含數據的數量。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM