• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Input validation that fails should try again.. VBscript

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

pik4chu

Senior Yellow Forum Rat
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. :D

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:
Thorw it in a while loop, or a similar construct.

Don't recall VB syntax off the top of my head, but this should be close enough.
Code:
boolIsValid = False
Do While Not boolIsValid 
	//stuff
	If valid Then
		boolIsValid=True
	End If
Loop
 
Yeah, while loops would be your best bet in doing this.

One thing I also noticed is that you're not echoing back to the user what they put in, it's a nice friendly way to code.
 
Yeah, while loops would be your best bet in doing this.

One thing I also noticed is that you're not echoing back to the user what they put in, it's a nice friendly way to code.

Been banging on the while loops for an hour now. However the script complains a lot when I have all the code inside the while loop (like saying it doesnt see 'expected loop' or 'expected while', somehow the if statements are messing with it and I cant figure out why) and it doesn't work at all if I don't put all the code inside the loop so having some fun issues there.

As for results to the user, that is slated for a future release. The 'user' for this script is intended for a more technical audience so I just wanted verification first in case of typos. The whole confirmation of entry will come when I get around to rewriting this in VB (or w/e I decide to use) and giving it a nice GUI.
 
I'm not very well versed in VBscript, so I'll try to help as much as possible.

Are you only getting the one input from the user?

Code:
WHILE again EQUALS true
     GET input
     TEST input
     IF input IS valid
          again EQUALS false
END WHILE
 
I'm not very well versed in VBscript, so I'll try to help as much as possible.

Are you only getting the one input from the user?

Code:
WHILE again EQUALS true
     GET input
     TEST input
     IF input IS valid
          again EQUALS false
END WHILE

nah i just posted only one for simplicity sake. there are about a dozen prompts right now. with about 6 more planned, though I will probably have the gui built before then I think.
 
I haven't tried this code, but I'd go for something like this:

Code:
Option Explicit

Call Main()

Sub Main()
	Dim FSO, WSHShell 'File system objects and commands	
	Set WSHShell = CreateObject("WScript.Shell")
	Set FSO = CreateObject("Scripting.FilesystemObject")
	
	Dim lngReturn,strLANIP
	strLANIP = 0
	Do
		strLANIP = fnPromptForLANIP(WSHShell,FSO)
	Loop While strLANIP = 0	
	
	'Script Cleanup
	Set WSHShell = Nothing
	Set FSO = Nothing
End Sub

Function fnPromptForLANIP(InShell,InFSO)

	Dim strLANIP, aLanIp, bIsValid, lngReturn
	Dim x, BoxOne
	strLANIP = InputBox("Please enter LAN IP address.", "Enter LAN IP")
	
	aLanIp = Split(strLanIP,".")
	bIsValid = True
	
	For each x in aLanIP
	    If x < 1 Or x > 255 Then
	        bIsValid = False
	    End If
	Next
	
	If bIsValid = False Then
		Call MsgBox("Sorry, " & strLANIP & " is not a valid address, please try again", 48, "Error, Bad Address!")
		strLANIP = 0
	End If
	
	fnPromptForLANIP = strLANIP

End Function
 
Last edited:
Ooo that looks exactly like what I need for this. Don't know if Ill have a chance today to test it out but thanks for the code! Feeling pretty positive about that. And if it works I can just copy pasta the function call for everything else I need. Thanks!

*edit* test ran it, had to remove the 'main()' stuff, that isnt needed in vbscript ;) after that it worked like a charm, thanks again!
 
Last edited:
Completed my sandbox testing and adapted it to be used with multiple prompts using only one function call each using Quigsby's code. Posted the revised code in the op for anyone interested.
 
Thanks for the update in OP pik4chu.

I made something similar to this using SCCM and RDP.

Was making some changes and your code gave me some great ideas.
 
Back