Problem solve Get help with specific problems with your technologies, process and projects.

Checking input by character to verify validity

I have an input field for an IP Address. The input needs to be verified if it is a valid IP Address; thus I want to check this input character by character to make sure it only includes digits and dots; then I will check the each number between two dots is between 0 and 255. The following code is for the second part, how can do the first part? I am new to VB.NET. In C++ if I define a string, I could use an array to get the character one by one.
 Dim str As String
        str = TextBox1.Text
        Dim counter As Integer = 1
        Dim results As String()
        results = str.Split(".")
        Dim IsIPAddress As Boolean

        IsIPAddress = True
        Dim scale As Int32
        scale = 256
        Dim i As Integer
        For i = 0 To results.GetUpperBound(0) Step 1
            If ((CInt(results(i) > scale)) Or (counter > 4)) Then
                IsIPAddress = False
                MsgBox("This is not a valid IP address, Please enter the correct one.")
                TextBox1.Text = Nothing
                Exit For
            End If
            counter += 1
You're missing a very important and key feature of the .NET framework generally used for input validation: Regular Expressions. Your scenario can be solved with one line of code (or 0 in ASP.NET) using them. The category of your question is set to ASP.NET, so let's first see how to use it in a Web application.

The first step is to drop a RegularExpressionValidator control next to your textbox. Set its ControlToValidate property to the textbox. Set the ErrorMessage to the message you want to give the user when the value is invalid. Finally, set the ValidationExpression to [0-255].[0-255].[0-255].[0-255]. In your code-behind, you can simply check for Page.IsValid to determine if the input was valid or not for all fields that have validators assigned.

Note that the regular expression simply states the groups of values between brackets. You may want to investigate regular expressions further as they provide a very powerful language for expressing requirements on an input string. You may want to take a look at the www.regexplib.com site, which contains a huge number of ready to use expressions for a number of problems.

The code you sent uses the MsgBox function, which is not available/usable on web applications (it's a WinForms message box that is used to display messages on a client application). So if your scenario is actually a WinForms application, here's the code you'd have to use:
' At the class level, a cached version of the compiled expression
Shared ipExpression As New Regex("[0-255].[0-255].[0-255].[0-255]", RegexOptions.Compiled)

' In your method that performs validation
    If Not ipExpression.IsMatch(TextBox1.Text) Then
      MsgBox("This is not a valid IP address, Please enter the correct one.")
      TextBox1.Text = Nothing
    End If

Dig Deeper on ASP.NET development best practices

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.