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
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