Input Mask/Validation for IP Address

Posted on 2004-11-16
Last Modified: 2012-05-05
What would be the proper input mask and validation rule for an IP Address?  I don't want to force all three digits.

For example you could say an IP address is always three digits but I want it to accept that, or

Question by:shanepresley
    LVL 58

    Accepted Solution

    I would not use an input mask. Rather, validate the input with something like:

    Function IsIP(pvarAdr) As Boolean

        Dim varNumbers As Variant
        Dim bytI As Byte

        If IsNull(pvarAdr) Then Exit Function
        varNumbers = Split(pvarAdr, ".")
        If UBound(varNumbers) <> 3 Then Exit Function
        For bytI = 0 To 3
            If Not IsNumeric(varNumbers(bytI)) Then Exit Function
            If InStr(varNumbers(bytI), " ") Then Exit Function
            If Val(varNumbers(bytI)) < 0 Then Exit Function
            If Val(varNumbers(bytI)) > 255 Then Exit Function
        Next bytI
        IsIP = True

    End Function

    To make thinks complete, you can use the KeyPress event to allow only digits, dots and backspace. This way, nothing can go wrong :)

    LVL 58

    Expert Comment

    Ah, well, here is the KeyPress event handler:

    Private Sub txtIPAddress_KeyPress(KeyAscii As Integer)

        Select Case KeyAscii
            Case vbKey0 To vbKey9
            Case Asc(".")
            Case vbKeyBack
            Case Else
                KeyAscii = 0
        End Select

    End Sub

    Good Luck

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now