2014-03-01 196 views
0

好的,我需要檢查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個不是空的,這是我爲檢查做的。我不這樣做每個文本框,因爲它是不需要的

+0

你知道你需要驗證的文本框的id嗎? – Gaurav

+0

這是問題1:它們從id =「txtb1到15」,id是1,2,4,8。但是如果我將1,2,4,8更改爲1-4,那麼我可以爲'txtb1 to txt4 bla bla next txtb'做一個'或類似的事情。 – d4d4u1

+0

那麼你想檢查的15箇中有哪4個? –

回答

0
  1. 添加唯一CssClass到所有4 TestBox
  2. 找到包含所有4 TextBox的父控件。
  3. 然後嘗試下面的代碼。

如果您已經申請validateemptyCssClassTextBox,如果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。 其次將以下函數添加到submit buttonOnClientClick屬性中。

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()將找到具有提供的過濾器參數的所有元素。在這種情況下,CSS類。如果由於有4個文本框而返回多個值,則循環遍歷結果並檢查可在$(this)元素中找到的單個找到的元素。如果您直接在$.find()內指定return,則它將從循環中返回而不是從function返回。

+0

是不是有這些4個文本框的ID,只是檢查他們? –

+0

如果需要驗證任何額外的文本框,那麼您可以添加cssclass,並且不需要更改代碼。 – Bharadwaj

+1

OP希望驗證在服務器端完成。如果是客戶端,那麼根本不需要做服務器。使用Java腳本就足夠了。 –

0

你可以維護一個你想驗證的數組。

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。

+1

它不是VB編碼 – Bharadwaj

+0

@Bharadwaj - 打開谷歌並鍵入 - c#到vb.net。打輸入:) – Gaurav

+0

我可以打進,但可以提問者同樣;) – Bharadwaj