- Joined
- Jan 17, 2003
- Location
- Centennial, Colorado
Ok so that title may not make much sense after all so let me try to explain.
What I am doing is in a vbscript it prompts for user input for some network information. Like hostname, IP address, gateway, subnet mask, etc. I have been updating said script to be more friendly and have added validation. Such that if a user enters an IP address that isn't valid, or numbers for a username it throws an error that it isn't valid. And all of that works fine. What I don't know how to do is if the user puts in something wrong I want the script to prompt them again for the same input rather than either continuing or canceling out entirely.
I don't need someone to write the whole thing for me or anything like that I just need to know what way would work best to do this.
Here is a snippet from one of the prompts that I was using for testing. Basically, it prompts for an IP, then checks that its a valid IP, if it is valid it will write it to the file (not shown in code) if it is not it will throw a message saying so. Now there are about a dozen prompts like this so as you could guess having the user redo them all when they screw up one is bad, as is skipping that entry entirely. So hopefully someone can help me out with the logic here please.
*another update* I have since recreated this script in full blown VB. people like it a lot though the input validation is done very differently in VB lol, so its been an interested learning experience and is still very basic (even more-so than whats in this script) but it has been a fun experience and I think I have truly found a new hobby.
For those interested, here is the revised, fixed and more streamlined snippet of code. Props to Quigsby for the base code. One catch is I haven't added any error handling for incorrect inputs, like if you enter a letter or special character into the prompts the script crashes on 'type mismatch'
What I am doing is in a vbscript it prompts for user input for some network information. Like hostname, IP address, gateway, subnet mask, etc. I have been updating said script to be more friendly and have added validation. Such that if a user enters an IP address that isn't valid, or numbers for a username it throws an error that it isn't valid. And all of that works fine. What I don't know how to do is if the user puts in something wrong I want the script to prompt them again for the same input rather than either continuing or canceling out entirely.
I don't need someone to write the whole thing for me or anything like that I just need to know what way would work best to do this.
Here is a snippet from one of the prompts that I was using for testing. Basically, it prompts for an IP, then checks that its a valid IP, if it is valid it will write it to the file (not shown in code) if it is not it will throw a message saying so. Now there are about a dozen prompts like this so as you could guess having the user redo them all when they screw up one is bad, as is skipping that entry entirely. So hopefully someone can help me out with the logic here please.
*another update* I have since recreated this script in full blown VB. people like it a lot though the input validation is done very differently in VB lol, so its been an interested learning experience and is still very basic (even more-so than whats in this script) but it has been a fun experience and I think I have truly found a new hobby.
Code:
Option Explicit
Dim FSO, WSHShell 'File system objects and commands
Dim strLanIp, aLanIp, boolIsValid 'variables for validation
Dim x, BoxOne
Set WSHShell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FilesystemObject")
strLanIP = InputBox("Please enter LAN IP address.", "Enter LAN IP")
aLanIp = Split(strLanIP,".")
boolIsValid = True
For each x in aLanIP
If x < 1 Or x > 255 Then
boolIsValid = False
End If
Next
If boolIsValid = False Then
BoxOne = MsgBox("Sorry, " & strLanIP & " is not a valid address, please try again", 48, "Error, Bad Address!")
End If
'Script Cleanup
Set WSHShell = Nothing
Set FSO = Nothing
For those interested, here is the revised, fixed and more streamlined snippet of code. Props to Quigsby for the base code. One catch is I haven't added any error handling for incorrect inputs, like if you enter a letter or special character into the prompts the script crashes on 'type mismatch'
Code:
Option Explicit
Dim FSO, WSHShell 'File system objects and commands
Set WSHShell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FilesystemObject")
''strPrompMsg is used to set the text displayed in the popup boxes
Dim strLANIP, strWANIP, strPromptMsg
strLANIP = ""
strWANIP = ""
Do
strPromptMsg = "LAN IP address"
strLANIP = fnPromptForIP(WSHShell,FSO)
Loop While strLANIP = ""
Do
strPromptMsg = "WAN IP address"
strWANIP = fnPromptForIP(WSHShell,FSO)
Loop While strWANIP = ""
MsgBox("strLanIP is: " & strLANIP)
MsgBox("strWanIP Is: " & strWANIP)
'Script Cleanup
Set WSHShell = Nothing
Set FSO = Nothing
Function fnPromptForIP(InShell,InFSO)
Dim strIP, aLanIp, bIsValid, strIsValid
Dim x, BoxOne
strIP = InputBox("Please enter the " & strPromptMsg & ".", "Enter IP Address")
aLanIp = Split(strIP,".")
bIsValid = True
For each x in aLanIP
If x < 1 Or x > 255 Then
bIsValid = False
End If
Next
If bIsValid = False Then ''Asks the user about entries that possibly arent valid
strIsValid = MsgBox("You entered " & strIP & ". This may not be a valid address. Use it anyways?", 4, "Error, Possible bad address!")
If strIsValid = 6 Then
fnPromptForIP = StrIP ''If they say yes then it uses the entry anyways
Else
StrIP = ""
End If
End If
fnPromptForIP = StrIP
End Function
Last edited: