简体   繁体   中英

Check the total of empty textbox in aspx

I have 5 textbox for in my aspx. When I click save button, how to I validate user at least fill in any 3 textbox? Is it possible to do that? Thanks

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>

Just put one validator and do this. It will work.

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
            }

        }

You could loop through the textbox and count the number that have data.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM