check if certain textboxes are empty

2019-09-05 08:40发布

问题:

ok, I needed to check if only 4 out of 15 textboxes were empty, but with no luck. I tried:

if txtbox1.text = "" then
lblerror1.visible=true
exitsub
else
bla bla
end if

But that left error text and didn't see the user entering the text in the textbox, so I looked and found string.isnullorwhitespace(string.value)... well, that didn't tell me how to use it so I searched more and found this: if string.isnullorwhitespace(textbox.text) then..

well that was it and here is the outcome. now if I could only get a for-next or do -while to only chaeck the 4 text fileds I need to check and not all textboxes.

ASPX page code:

<asp:Label ID="lblerror" runat="server" Text="Page error" Visible="false" forecolor="red"/><br />
<asp:TextBox ID="txtName" runat="server" Width="100px" /><asp:Label ID="nameblankerror" runat='server' Text="cannot be blank" ForeColor="Red" Visible="false" /><br />
<asp:TextBox ID="txtcompname" runat="server" Width="100px" /><asp:Label ID="compblankerror" runat='server' Text="cannot be blank" ForeColor="Red" Visible="false" /><br />
<asp:Button ID="btnSubmit" runat="server" Text="submit" /><br />
<asp:Label ID="lbl1" runat="server" Visible="true" Text="TextBox 1: " /><asp:label ID="lblname" runat="server" /><br />
<asp:Label ID="lbl2" runat="server" Visible="true" Text="TextBox 2: " /><asp:label ID="lblCompName" runat="server" />

and for the backend code:

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    'test to see if certain the fields are blank
    Dim str1 As String = txtName.Text
    Dim str2 As String = txtcompname.Text
    Dim CatchMe As Integer = 0

    If String.IsNullOrWhiteSpace(txtName.Text) Then
        nameblankerror.Visible = True
        CatchMe += 1
    Else
        nameblankerror.Visible = False
        lblname.text = str1.Trim()
        CatchMe += 0
    End If
    If String.IsNullOrWhiteSpace(txtcompname.Text) Then
        compblankerror.Visible = True
        CatchMe += 1
    Else
        compblankerror.Visible = False
        lblCompName.Text = str2.Trim()
        CatchMe += 0
    End If
    If CatchMe = 0 Then
        'do something like process SQL Command
        lblerror.Visible = False
        Exit Sub
    ElseIf CatchMe <> 0 Then
        'return to page and process nothing
        lblerror.Visible = True
        Exit Sub
    End If
End Sub

so that is it. a simple, easy to follow check for certain textboxes out of a bunch. Like I stated above if I could figure out how to check only certain textboxes and not all textboxes, that and make the code shorter would be great. I put in a catchme so that if one box was filled in it wouldn't show the user that they need to fill that one also (in Error), but would catch the other empty textboxes. to make it clear, if I have 15 textboxes, but only care that 4 of them are not empty, this is what I do for the check. I don't do this for every textbox as it is not needed

回答1:

  1. Add unique CssClass to all 4 TestBox.
  2. Find the parent control which holds all 4 TextBox.
  3. And then try the following code.

If you have applied validateempty as CssClass for TextBox and if Ctrl1 is the parent control which holds the textboxes then.

For Each ctrl as Control In Ctrl1.Controls
 Dim txt as TextBox = TryCast(ctrl, TextBox)
 If txt IsNot Nothing And txt.CssClass = "validateempty" And txt.Text.Trim() = "" Then 
  'Your choice of code hear.
 End If

Next

Using JQuery you can find an element by its cssclass name. First add the JQuery reference to your page You can find it hear. Second add the following function to OnClientClick attribute of the submit button.

function validate(){
//Since you have added cssclass for the textboxes as "validateempty" then
 var ret = true;
 $.find(".validateempty").each(function(){
 if(ret){
  if($(this).val().trim() == ""){
   alert("Enter the value.");
   $(this).focus();
   ret = false;
  }
 }
 });
return ret;
}

$.find() will find all elements with the provided filter parameter. In this case css class. If there are more than one value is returned as there are 4 text boxes, then loop through the result and check individual found element which can be found in $(this) element. If you specify return directly inside the $.find(), then it will return from the loop not from function.



回答2:

You can maintain an array of Id's you want to validate.

   String[] txtboxToValidate = { "txtb1", "txtb2", "txtb4", "txtb8" };

    foreach (Control c in Page.Controls)
    {
        if (c is TextBox)
        {
            int pos = Array.IndexOf(txtboxToValidate, c.ID);
            TextBox txtBox = (TextBox)c;
            if (pos > -1)
            {
                if (String.IsNullOrEmpty(txtBox.Text))
                {
                    //Write your logic how you want to throw your error.
                }

            }
        }
    }

It's in c# but logic remains same. You can convert it to VB using online code converters etc.