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
You can maintain an array of Id's you want to validate.
It's in c# but logic remains same. You can convert it to VB using online code converters etc.
CssClass
to all 4TestBox
.TextBox
.If you have applied
validateempty
asCssClass
forTextBox
and ifCtrl1
is the parent control which holds the textboxes then.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 toOnClientClick
attribute of thesubmit button
.$.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 specifyreturn
directly inside the$.find()
, then it will return from the loop not fromfunction
.