DragonRider
DragonRider

Reputation: 13

VBScript - Verify input is IP, 3-digits per octet

I am a student learning VBScript. It was only a few days ago [grin], that I cut my teeth on programming in BASIC, DOS and Machine Code, so the basic concepts are not foreign, it has just been a bit.

Caveat - this is part of a graded assignment, guidance and understanding of the code is what is mainly being requested.

Trying to write a script for user input of IP address where each octet is in 3-digit format (leading zeros if necessary). This is the starting input

' Get IP address to convert
WScript.StdOut.Write("Please Enter your IP address (3 digits per octet) -> ")
strIP = WScript.StdIn.ReadLine()

Trying to get the error-checking and having problems. Tried various ways but none seem to offer the desired output, if not fail with various VBScript errors when run.

Version 1:

Dim sMatch(15)
if sMatch = strIP Like "###.###.###.###" Then
   Wscript.Echo "The strings are equal."
Else
   Wscript.Echo "The strings are not equal."
' WScript.Echo sMatch
End If

Version 2:

String str = StrComp("strIP", ""\###.\###.\###.\###"",vbTextCompare)
If str < 0 or str > 0 then WScript.StdOut.WriteLine "Improper IP format."

Version 3:

iFirstOct = Left(strIP,3)

z1 = strComp(iFirstOct, "###", 1)
WScript.Echo "z1 -> " & z1
WScript.Echo "IP -> " & iFirstOct
If z1 < 1 or z1 > 1 Then 
  WScript.Echo " Error in input !" 
  WScript.quit
End If

I sort of like Version 3 as I can subroutine this, but not sure which is a viable option. Any assistance to identify where I am erroring, and how, will be appreciated.

Caveat - this is part of a graded assignment, guidance and understanding of the code is what is mainly being requested.

-- Edit - 16-FEB-21 --

Étienne,

Here is the final script. I have run the different error scenario's and it appears to function the way I need it to. Thank you for your assistance.

' IP_func.vbs
'
'  By: Étienne Laneville
'    Minor modifications by DragonRider
'  From: https://stackoverflow.com/questions/66177237/vbscript-verify-input-is-ip-3-digits-per-octet
'
'
WScript.StdOut.Write("Please Enter your IP address (3 digits per octet) -> ")
strIP = WScript.StdIn.ReadLine()
WScript.Echo vbCrLf & " Input -> " & vbTab & strIP & vbCrlf

' Call error-checking function
CheckIP(strIP)


Function CheckIP(strIP)
    Dim arrOctets
    Dim iCounter
    Dim sOctet
    
    ' Check for periods
    If InStr(strIP, ".") = 0 Then
        CheckIP = False
        WScript.Echo " Incorrect number of octets. Please try again."
        Exit Function 
    End If

    ' Split string on periods
    arrOctets = Split(strIP, ".")
    
    ' Check that correct number of octets has been received
    If UBound(arrOctets) <> 3 Then
        CheckIP = False
        WScript.Echo " Incorrect number of octets. Please try again."
        Exit Function
    End If
    
    ' Check each octet
    For iCounter = 1 To 4
        
        sOctet = arrOctets(iCounter - 1)
        
        ' Check if it's numeric
        If Not IsNumeric(sOctet) Then
            CheckIP = False
            WScript.Echo " Improper IP Address. Please try again."
            Exit Function
        End If
        
        ' Check if 3 digits
'        Dim three As String
        If Not Len(sOctet) = 3 Then
            CheckIP = False
            WScript.Echo " 3-Digits per octet. Please try again."
            Exit Function
        End If
        
        ' Check for proper values
        If CInt(sOctet) > 255 Then
            CheckIP = False
            WScript.Echo " Octet out of range. Please try again."
            Exit Function
        End If
        
        If CInt(sOctet) < 0 Then
            CheckIP = False
            WScript.Echo " Improper IP Address. Please try again."
            Exit Function
        End If
        
    Next
        
    CheckIP = True

End Function

Upvotes: 1

Views: 350

Answers (2)

Hackoo
Hackoo

Reputation: 18847

You can also check the validity of an IP Address using Regular Expression with this function :

Option Explicit
Dim  Title,IP
Title = "Test the validity of an IP address"
IP = InputBox("Please enter an IP Address",Title,"172.16.18.21")
If Is_Valid(IP) = True Then
    MsgBox IP & " is a Valid IP Address",64,Title
Else
    MsgBox IP & " is Not a Valid IP Address",16,Title
End if
'------------------------------------------------------------------------------------
Function Is_Valid(ip)
    Dim RegularExpressionObject
    Set RegularExpressionObject = New RegExp
    With RegularExpressionObject
        .Pattern = "^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$"
        .IgnoreCase = False
        If .Test(ip)= True then
            Is_Valid = True
        end if
    End With
End Function
'------------------------------------------------------------------------------------

Upvotes: 1

&#201;tienne Laneville
&#201;tienne Laneville

Reputation: 5031

Here's a simple function that breaks things down into simple operations:

Function CheckIP(strIP)
    Dim arrOctets
    Dim iCounter
    Dim sOctet
    
    ' Check for periods
    If InStr(strIP, ".") = 0 Then
        CheckIP = False
        Exit Function
    End If

    ' Split string on periods
    arrOctets = Split(strIP, ".")
    
    ' Check that correct number of octets has been received
    If UBound(arrOctets) <> 3 Then
        CheckIP = False
        Exit Function
    End If
    
    ' Check each octet
    For iCounter = 1 To 4
        
        sOctet = arrOctets(iCounter - 1)
        
        ' Check if it's numeric
        If Not IsNumeric(sOctet) Then
            CheckIP = False
            Exit Function
        End If
        
        ' Check for proper values
        If CInt(sOctet) > 255 Then
            CheckIP = False
            Exit Function
        End If
        
        If CInt(sOctet) < 0 Then
            CheckIP = False
            Exit Function
        End If
        
    Next
        
    CheckIP = True

End Function

Upvotes: 1

Related Questions