繁体   English   中英

检查某些文本框是否为空

[英]check if certain textboxes are empty

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

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

但这留下了错误文本,没有看到用户在文本框中输入文本,因此我查看并发现了string.isnullorwhitespace(string.value)...好吧,这没有告诉我如何使用它,所以我进行了搜索更多,发现:如果是string.isnullorwhitespace(textbox.text)则。

就是这样,这就是结果。 现在,如果我只能得到一个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个不为空,这就是我要做的检查。 我不需要为每个文本框都这样做,因为它不是必需的

  1. 将唯一的CssClass添加到所有4个TestBox
  2. 找到包含所有4个TextBox的父TextBox
  3. 然后尝试以下代码。

如果您已经申请validateempty作为CssClassTextBox ,如果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引用添加到您的页面, 您可以找到它 其次,将以下函数添加到OnClientClick submit button OnClientClick属性。

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()将查找具有提供的filter参数的所有元素。 在这种情况下,css类。 如果返回的值不止一个,因为有四个文本框,则返回结果,然后遍历结果并检查可以在$(this)元素中找到的单个找到的元素。 如果直接在$.find()内部指定return ,那么它将从循环而不是从function返回。

您可以维护要验证的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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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