Validation of userinput

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rwarren1
    New Member
    • Apr 2010
    • 4

    Validation of userinput

    What is the best way to validate a users input? I need to validate a "Name" entered in a textbox, as well as an enetered "Score". Could someone please help me with this.
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    you need to write a function for the purpose with all the validation logic implemented.

    Comment

    • rwarren1
      New Member
      • Apr 2010
      • 4

      #3
      Originally posted by debasisdas
      you need to write a function for the purpose with all the validation logic implemented.
      Thank you for your reply. Could you give me an idea of how to write the function?What I have is:
      Code:
      Private Sub txtNewName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles textNewName.Validating
      
        If Not (txtNewName.Text Like "A-Za-z") Then
          e.Cancel = True
      
          txtNewName.Select(0, txtNewName.Text.Length)
      
      MessageBox.Show (txtNewName, "Invalid name")
        End If
      End Sub
      Last edited by Frinavale; Apr 13 '10, 03:21 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

      Comment

      • vb5prgrmr
        Recognized Expert Contributor
        • Oct 2009
        • 305

        #4
        Wrong forum, you need to be in the VB.NET forum...

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Are you developing an ASP.NET MVC web application?

          Comment

          • rwarren1
            New Member
            • Apr 2010
            • 4

            #6
            Originally posted by Frinavale
            Are you developing an ASP.NET MVC web application?
            I was told I need to be in VB.Net forum. I am developing a program in visual studio 2008. Thanks

            Comment

            • rwarren1
              New Member
              • Apr 2010
              • 4

              #7
              Validating user input from a textbox. Alpha characters

              I'm not sure if this is the correct code to use for validating user input. Would someone correct me please. Thank you.

              Code:
              Private Sub txtNewName_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles textNewName.Validating
               
                If Not (txtNewName.Text Like "A-Za-z") Then
                  e.Cancel = True
               
                  txtNewName.Select(0, txtNewName.Text.Length)
               
              MessageBox.Show (txtNewName, "Invalid name")
                End If
              End Sub

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                When you created the project that you're working on....did you create it as an MVC ASP.NET project or did you create it as a normal ASP.NET application/website?

                I suggest you create a class that you can use to validate your user input with.
                For example:
                Code:
                Class myValidation 
                
                  Public Shared Function UserNameIsValid(ByVal userName As String)
                    Dim isValid As Boolean =  False
                    If (someRequirementsAreMet) Then
                      isValid = True
                    End If
                
                    Return isValid
                  End Function
                
                '...other functions
                End Class
                Now, when the user tries to submit the data to be saved you can validate the data before you actually save it.

                In your page code you would have:
                Code:
                  'In Button Click event:
                  If (myValidation.UserNameIsValid(txt_username.Text) AndAlso ...other validation Then
                    'update database or whatever you need to do
                  Else
                    'indicate to the user that there were errors.
                  End If
                -Frinny

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  rWarren1: You have started 4 separate threads for this same topic/issue. Please stop doing that. It makes it hard to coordinate efforts to help you.

                  []I was told I need to be in VB.Net forum. I am developing a program in visual studio 2008. [/]

                  Thanks Visual Studio supports several different programming languages. There are forums here for each language: C-Sharp, Visual Basic, Visual C++ and so on. In the future you need to post your questions in the correct forum if you expect the most experienced people to see them so they can help you.

                  Comment

                  Working...