Home > Ask the Microsoft .Net Development Experts > ASP.NET and Web Development Questions & Answers > Checking input by character to verify validity
Ask The Win Development Expert: Questions & Answers
EMAIL THIS

Checking input by character to verify validity

Daniel Cazzulino EXPERT RESPONSE FROM: Daniel Cazzulino

Pose a Question
Other Win Development Categories
Meet all Win Development Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 29 November 2004
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
        Next

>
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


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
ASP.NET and Web Development
Top ASP.NET expert advice from Danny Cazzulino
Access VB .NET Windows control on client side
Drop down list server control creates error message
Take care with InitializeComponent
Calling via command prompt from ASP.NET
Screen capturing
How to retrieve the MAC address of a client using ASP.NET
How to sort a dropdown list in ASP.NET
Accessing session variables in different pages
Automatically closing a .exe

ASP.NET
Top ASP.NET expert advice from Danny Cazzulino
Access VB .NET Windows control on client side
Drop down list server control creates error message
How to retrieve the MAC address of a client using ASP.NET
How to sort a dropdown list in ASP.NET
Accessing session variables in different pages
Automatically closing a .exe
Accessing a control on an independent application
Referencing an old Crystal Reports version
Redirecting results to a text file

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Windows Development - White Papers, News and Expert Advice
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts