Check if form fields are empty

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bluenose
    New Member
    • Apr 2012
    • 56

    Check if form fields are empty

    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.

    Code:
    Sub Main()
    
            Dim strEmail As String = Nothing
            Dim password As String = Nothing
    
            If String.IsNullOrEmpty(strEmail) Then
    
                MsgBox("Please type in a valid email")
    
            End If
    
            If String.IsNullOrEmpty(password) Then
    
                MsgBox("Please type in a password")
    
            End If
    
        End Sub
    Thank you.
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    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:

    Code:
    Public Class Form1
        Dim strEmail As String = Nothing
        Dim password As String = Nothing
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If String.IsNullOrEmpty(strEmail) Then
                MsgBox("Email is empty")
            ElseIf String.IsNullOrEmpty(password) Then
                MsgBox("Password is empty")
            End If
        End Sub
    
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            strEmail = TextBox1.Text
        End Sub
    
        Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
            password = TextBox2.Text
        End Sub
    End Class

    Comment

    • Bluenose
      New Member
      • Apr 2012
      • 56

      #3
      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:

      Code:
      Public Class LogonForm
      
      
          Dim strEmail As String = Nothing
          Dim password As String = Nothing
      
              Private Sub LogonBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogonBtn.Click)
      
              If String.IsNullOrEmpty(strEmail) Then
      
                  MsgBox("Please type in a valid email")
      
              ElseIf String.IsNullOrEmpty(password) Then
      
                  MsgBox("Please type in a password")
      
              End If
      
          End Sub
      
          Private Sub strEmail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmail.TextChanged
      
              strEmail = strEmail.Text
      
          End Sub
      
          Private Sub password_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
      
              password = password.Text()
      
          End Sub
      
      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:

      Code:
      strEmail.Text
      password.Text()
      Error: 'Text is not a member of String'

      Thanks again for your time!
      Attached Files

      Comment

      • Luk3r
        Contributor
        • Jan 2014
        • 300

        #4
        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):

        Code:
        Public Class LogonForm
        
             Dim strEmail As String = Nothing
             Dim password As String = Nothing
         
             Private Sub LogonBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogonBtn.Click)
                 If String.IsNullOrEmpty(strEmail) Then
                     MsgBox("Please type in a valid email")
                 ElseIf String.IsNullOrEmpty(password) Then
                     MsgBox("Please type in a password")
                 End If
             End Sub
         
             Private Sub strEmailButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmail.TextChanged
                 strEmail = strEmail.Text
             End Sub
         
             Private Sub passwordButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
                 password = password.Text()
             End Sub
         
         End Class

        Comment

        • Bluenose
          New Member
          • Apr 2012
          • 56

          #5
          Hello Luk3r

          Thanks again.

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

          Code:
          <asp:Button ID="strEmailButton" runat="server" Text="Button" />
          
          <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:

          Code:
          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 Files

          Comment

          • Luk3r
            Contributor
            • Jan 2014
            • 300

            #6
            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:

            Code:
                 Private Sub strEmailButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmail.TextChanged
                      strEmail = strEmailButton.Text
                  End Sub
             
                  Private Sub passwordButton_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
                      password = passwordButton.Text
                  End Sub
            I would venture to guess that the textbox names that I chose are also misleading. They should be something more like "strEmailTextBo x" and "passwordTextBo x" instead of "strEmailButton " and "passwordButton ". That updated code would look like this:

            Code:
                  Private Sub strEmailTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmail.TextChanged
                      strEmail = strEmailTextBox.Text
                  End Sub
             
                  Private Sub passwordTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
                      password = passwordTextBox.Text
                  End Sub

            Comment

            • Bluenose
              New Member
              • Apr 2012
              • 56

              #7
              Sorry for the delay in replying.

              Please see attachment after I incorporated your code.

              I have placed these in my aspx file:

              Code:
              <asp:TextBox ID="strEmailTextBox" runat="server"></asp:TextBox>
              
              <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:

              Code:
              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:

              Code:
               <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:

              Code:
               
              Dim strEmailValue As String = Nothing 'variable
              Dim passwordValue As String = Nothing 'variable
              
              Dim strEmailTextBox As Object 'TextBox itself
              Dim passwordTextBox As Object 'TextBox itself
              Attached Files

              Comment

              • Luk3r
                Contributor
                • Jan 2014
                • 300

                #8
                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 "strEmailTextBo x" and "passwordTextBo x".

                Example:
                Code:
                Private Sub strEmailTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strEmailTextBox.TextChanged
                     strEmail = strEmailTextBox.Text
                End Sub
                
                Private Sub passwordTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles passwordTextBox.TextChanged
                    password = passwordTextBox.Text
                End Sub

                Comment

                Working...