By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Check if form fields are empty

P: 56
Hello

I have a two-field form (email and password). If I want to check that both fields have been completed by the user, is this likely to work?

strEmail is the ID of the email field and password is the ID of the password field. I don't get any debug errors in Visual Studio 2013 for Web.

Expand|Select|Wrap|Line Numbers
  1. Sub Main()
  2.  
  3.         Dim strEmail As String = Nothing
  4.         Dim password As String = Nothing
  5.  
  6.         If String.IsNullOrEmpty(strEmail) Then
  7.  
  8.             MsgBox("Please type in a valid email")
  9.  
  10.         End If
  11.  
  12.         If String.IsNullOrEmpty(password) Then
  13.  
  14.             MsgBox("Please type in a password")
  15.  
  16.         End If
  17.  
  18.     End Sub
Thank you.
May 4 '16 #1

✓ answered by Luk3r

Bluenose,
These errors come from you having variables and controls named exactly the same. You cannot do this. You would have to do something like the following (notice the button names):

Expand|Select|Wrap|Line Numbers
  1. Public Class LogonForm
  2.  
  3.      Dim strEmail As String = Nothing
  4.      Dim password As String = Nothing
  5.  
  6.      Private Sub LogonBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogonBtn.Click)
  7.          If String.IsNullOrEmpty(strEmail) Then
  8.              MsgBox("Please type in a valid email")
  9.          ElseIf String.IsNullOrEmpty(password) Then
  10.              MsgBox("Please type in a password")
  11.          End If
  12.      End Sub
  13.  
  14.      Private Sub strEmailButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmail.TextChanged
  15.          strEmail = strEmail.Text
  16.      End Sub
  17.  
  18.      Private Sub passwordButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
  19.          password = password.Text()
  20.      End Sub
  21.  
  22.  End Class
  23.  

Share this Question
Share on Google+
7 Replies


100+
P: 299
I recreated what you're trying to do with a button (button1) and two textboxes (textbox1 and textbox2). You likely will not want to run two If.. statements because your users will be prompted with unnecessary prompts:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim strEmail As String = Nothing
  3.     Dim password As String = Nothing
  4.  
  5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.         If String.IsNullOrEmpty(strEmail) Then
  7.             MsgBox("Email is empty")
  8.         ElseIf String.IsNullOrEmpty(password) Then
  9.             MsgBox("Password is empty")
  10.         End If
  11.     End Sub
  12.  
  13.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  14.         strEmail = TextBox1.Text
  15.     End Sub
  16.  
  17.     Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
  18.         password = TextBox2.Text
  19.     End Sub
  20. End Class
May 5 '16 #2

P: 56
Hello Luk3r

Many thanks for your reply and code.

I have put it into Visual Studio (in it's own class, as you have it, in my logon.aspx.vb file) and made a couple of amendments to conform with the name of my button and text-boxes (I couldn't include it in my main class and logon button because I got a few errors).

It looks like this:

Expand|Select|Wrap|Line Numbers
  1. Public Class LogonForm
  2.  
  3.  
  4.     Dim strEmail As String = Nothing
  5.     Dim password As String = Nothing
  6.  
  7.         Private Sub LogonBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogonBtn.Click)
  8.  
  9.         If String.IsNullOrEmpty(strEmail) Then
  10.  
  11.             MsgBox("Please type in a valid email")
  12.  
  13.         ElseIf String.IsNullOrEmpty(password) Then
  14.  
  15.             MsgBox("Please type in a password")
  16.  
  17.         End If
  18.  
  19.     End Sub
  20.  
  21.     Private Sub strEmail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmail.TextChanged
  22.  
  23.         strEmail = strEmail.Text
  24.  
  25.     End Sub
  26.  
  27.     Private Sub password_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
  28.  
  29.         password = password.Text()
  30.  
  31.     End Sub
  32.  
  33. End Class
I have replaced your Button1 with my LogonBtn, your TextBox1.Text with my strEmail.Text, and your TextBox2.Text with my password.Text.

I am getting a few errors, which are almost certainly my fault (please see attachment).

The three 'Handles' errors refer to a 'Handles clause requires a WithEvents variable defined in the containing type or one of its base types' (whatever that means), and the other two errors (blue underlines) here:

Expand|Select|Wrap|Line Numbers
  1. strEmail.Text
  2. password.Text()
  3.  
Error: 'Text is not a member of String'

Thanks again for your time!
Attached Images
File Type: jpg Bytes.jpg (44.0 KB, 74 views)
May 5 '16 #3

100+
P: 299
Bluenose,
These errors come from you having variables and controls named exactly the same. You cannot do this. You would have to do something like the following (notice the button names):

Expand|Select|Wrap|Line Numbers
  1. Public Class LogonForm
  2.  
  3.      Dim strEmail As String = Nothing
  4.      Dim password As String = Nothing
  5.  
  6.      Private Sub LogonBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogonBtn.Click)
  7.          If String.IsNullOrEmpty(strEmail) Then
  8.              MsgBox("Please type in a valid email")
  9.          ElseIf String.IsNullOrEmpty(password) Then
  10.              MsgBox("Please type in a password")
  11.          End If
  12.      End Sub
  13.  
  14.      Private Sub strEmailButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmail.TextChanged
  15.          strEmail = strEmail.Text
  16.      End Sub
  17.  
  18.      Private Sub passwordButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
  19.          password = password.Text()
  20.      End Sub
  21.  
  22.  End Class
  23.  
May 6 '16 #4

P: 56
Hello Luk3r

Thanks again.

I have copied and pasted your code - thanks - into Visual Studio (VS) and added two buttons to my aspx page:

Expand|Select|Wrap|Line Numbers
  1. <asp:Button ID="strEmailButton" runat="server" Text="Button" />
  2.  
  3. <asp:Button ID="passwordButton" runat="server" Text="Button" />
These are objects, aren't they, like my strEmail text-box, who values we know (their IDs) in a way that we don't know about variables.

I am attaching a screenshot of what VS shows with the code you kindly posted. The errors seems to be the same as before.

Yes, I can see what you mean about confusion and not helped by the error 'Text is not a member of String'.

I get the feeling that the errors shown in the screenshot are not the real errors. For example, there is nothing wrong with:

Expand|Select|Wrap|Line Numbers
  1. Handles LogonBtn.Click
because I have used it elsewhere in another class (same aspx.vb file, but another class).

If I were to keep strEmail as the ID of the email form field and declare strEmailValue as the variable, how would that pan out?

How would I set the code out in that case?

Thanks again
Attached Images
File Type: jpg Luk3r.jpg (31.0 KB, 71 views)
May 6 '16 #5

100+
P: 299
My apologies, I should have written the code to reflect the name changes. Your code is still trying to set a variable to the name of a textbox that doesn't exist. This code is more accurate:

Expand|Select|Wrap|Line Numbers
  1.      Private Sub strEmailButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmail.TextChanged
  2.           strEmail = strEmailButton.Text
  3.       End Sub
  4.  
  5.       Private Sub passwordButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
  6.           password = passwordButton.Text
  7.       End Sub
  8.  
I would venture to guess that the textbox names that I chose are also misleading. They should be something more like "strEmailTextBox" and "passwordTextBox" instead of "strEmailButton" and "passwordButton". That updated code would look like this:

Expand|Select|Wrap|Line Numbers
  1.       Private Sub strEmailTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmail.TextChanged
  2.           strEmail = strEmailTextBox.Text
  3.       End Sub
  4.  
  5.       Private Sub passwordTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
  6.           password = passwordTextBox.Text
  7.       End Sub
  8.  
May 6 '16 #6

P: 56
Sorry for the delay in replying.

Please see attachment after I incorporated your code.

I have placed these in my aspx file:

Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox ID="strEmailTextBox" runat="server"></asp:TextBox>
  2.  
  3. <asp:TextBox ID="passwordTextBox" runat="server"></asp:TextBox>
The code looks more self-explanatory but a couple of errors remain. The error I find baffling is this one:

Expand|Select|Wrap|Line Numbers
  1. Handles LogonBtn.Click
where LogonBtn is definitely in my aspx file and, indeed, allows me to logon via the code I have in my other class:

Expand|Select|Wrap|Line Numbers
  1.  <asp:Button ID="LogonBtn" runat="server" Text="Send" CssClass="submit" OnClick="LogonBtn_Click" />
At the very beginning of your code I have these declarations to make it easier for me to read:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim strEmailValue As String = Nothing 'variable
  3. Dim passwordValue As String = Nothing 'variable
  4.  
  5. Dim strEmailTextBox As Object 'TextBox itself
  6. Dim passwordTextBox As Object 'TextBox itself
Attached Images
File Type: jpg gettingThere.jpg (33.0 KB, 58 views)
May 6 '16 #7

100+
P: 299
Sorry, I keep assuming that you will notice some of these things yourself and I shouldn't. First things first, you need to modify your subs similar to the below code. Anywhere that "strEmail" or "password" used to be (as a Sub) needs modified to "strEmailTextBox" and "passwordTextBox".

Example:
Expand|Select|Wrap|Line Numbers
  1. Private Sub strEmailTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmailTextBox.TextChanged
  2.      strEmail = strEmailTextBox.Text
  3. End Sub
  4.  
  5. Private Sub passwordTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles passwordTextBox.TextChanged
  6.     password = passwordTextBox.Text
  7. End Sub
  8.  
May 9 '16 #8

Post your reply

Sign in to post your reply or Sign up for a free account.