Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Input Mask/Validation for IP Address

Posted on 2004-11-16
Medium Priority
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
  • 2
LVL 58

Accepted Solution

harfang earned 1000 total points
ID: 12600587
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

ID: 12600614
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

564 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