检查某些文本框是否为空

时间:2014-03-01 04:11:21

标签: asp.net vb.net textbox string

好吧,我需要检查15个文本框中只有4个是空的,但没有运气。我试过了:

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

但是留下了错误文本而没有看到用户在文本框中输入文本,所以我查看并找到了string.isnullorwhitespace(string.value)... 好吧,那没有告诉我如何使用它所以我搜索更多,发现这个: if string.isnullorwhitespace(textbox.text)then ..

好吧就是这样,这就是结果。现在如果我只能得到一个for-next或do -while只能查看4个文本文件我需要检查而不是所有文本框。

ASPX页面代码:

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

以及后端代码:

    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

就是这样。一个简单,易于检查的特定文本框。 就像我上面所说的,如果我能弄清楚如何只检查某些文本框而不是所有文本框,那么使代码更短就会很棒。我放入一个catchme,以便如果一个盒子被填入它不会向用户显示他们也需要填充那个(在Error中),但会捕获其他空文本框。 要说清楚,如果我有15个文本框,但只关心其中4个不是空的,这就是我做的检查。我没有为每个文本框执行此操作,因为它不需要

2 个答案:

答案 0 :(得分:0)

  1. 为所有4 CssClass添加唯一TestBox
  2. 找到包含所有4 TextBox
  3. 的父控件
  4. 然后尝试以下代码。
  5. 如果您已为validateempty申请CssClass TextBox,而Ctrl1是包含文本框的父控件。

    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
    

    使用JQuery,您可以通过其cssclass名称找到元素。 首先将JQuery引用添加到页面You can find it hear。 其次,将以下函数添加到OnClientClick的{​​{1}}属性。

    submit button

    $.find()将使用提供的过滤器参数找到所有元素。在这种情况下css类。如果有4个文本框,则返回多个值,然后遍历结果并检查可在$(this)元素中找到的单个找到的元素。如果您直接在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; } 内指定return,则它将从循环返回,而不是从$.find()返回。

答案 1 :(得分:0)

您可以维护要验证的Id数组。

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

            }
        }
    }

它在c#中但逻辑仍然相同。您可以使用在线代码转换器等将其转换为VB。