How to check input range - basic loop question 
Author Message
 How to check input range - basic loop question

Forgive me to being new to VB script, but I'm writing a logon script that
will present the user a choice via InputBox upon logging in and connect
various resources based on the users choice.  My script works OK except it
doesn't take into account invalid input range.  Basically the valid input
range is based on 2 variable (varMax and varMin say - currently valid
choices are 1-9 as numerical input).  I want to keep prompting the user for
input until the input is valid (1-9) but I can't seem to get VB to loop
properly.  I am including a code snipet that I think should work but
doesn't.  Any
advice on what's wrong is appreciated.  Feel free to suggest different
(any!) way to validate the input.  Thanks!

*** start vb code ***
' the following code will infinately loop allthough it should exit if a
value 1 through 9 is entered

varMin = 1
varMax = 9
varInput = 0

Do
  TryAgain = "No"
  varInput = InputBox("please select a number 1-9")
  varInput = Trim(varInput)
  If varInput < varMin or varInput > varMax Then
    wscript.echo "varInput= " & varInput & " -Invalid Selection"
    TryAgain = "Yes"
  End If
Loop While TryAgain = "Yes"
wscript echo = "you have exited the loop, varInput = " & varInput

*** end vb code ***



Sun, 31 Jul 2005 05:02:08 GMT  
 How to check input range - basic loop question
Hi,

Your code seems fine.  It just doesn't take into account the fact that all
VBS variables are subtyped variants.  Your input box return is a string
subtype, which you are comparing to a numeric subtype.  While arithmetic
(+ - * etc.) or logical (AND OR etc.) or string (& etc.) operators will
force a conversion, comparison operators do not force the conversion, and a
string is always less than a numeric.

Try:

If (CInt(varInput)<varMin) OR (CInt(varInput)>varMax) Then

Or:

If ((varInput +0)<varMin) OR ((varInput +0)>varMax) Then

Joe Earnest


Quote:
> Forgive me to being new to VB script, but I'm writing a logon script that
> will present the user a choice via InputBox upon logging in and connect
> various resources based on the users choice.  My script works OK except it
> doesn't take into account invalid input range.  Basically the valid input
> range is based on 2 variable (varMax and varMin say - currently valid
> choices are 1-9 as numerical input).  I want to keep prompting the user
for
> input until the input is valid (1-9) but I can't seem to get VB to loop
> properly.  I am including a code snipet that I think should work but
> doesn't.  Any
> advice on what's wrong is appreciated.  Feel free to suggest different
> (any!) way to validate the input.  Thanks!

> *** start vb code ***
> ' the following code will infinately loop allthough it should exit if a
> value 1 through 9 is entered

> varMin = 1
> varMax = 9
> varInput = 0

> Do
>   TryAgain = "No"
>   varInput = InputBox("please select a number 1-9")
>   varInput = Trim(varInput)
>   If varInput < varMin or varInput > varMax Then
>     wscript.echo "varInput= " & varInput & " -Invalid Selection"
>     TryAgain = "Yes"
>   End If
> Loop While TryAgain = "Yes"
> wscript echo = "you have exited the loop, varInput = " & varInput

> *** end vb code ***



Sun, 31 Jul 2005 06:00:52 GMT  
 How to check input range - basic loop question
Hi Again,

Just noting that, also, you don't have a way to allow the user to cancel
out.  If this is not intended, a cancel at an InputBox returns a null string
( "" ).  A null string or a non-numeric input by the user will create a
runtime error, if you use the CInt or forced conversions that I suggested.
You may want error-trapping code.  Consider something like the following:

---

nMin = 1
nMax = 9

Do
  sInput= InputBox(sTryAgain _
    & "Please input a number 1-9.", "Number Selection", sInput)
  If (sInput="") Then nInput= 0: Exit Do
  On Error Resume Next
    nInput= CInt(Trim(sInput)): If Err Then nInput= 0
  On Error GoTo 0
  If NOT ((nInput<nMin) OR (nInput>nMax)) Then Exit Do
  sTryAgain= "Invalid Input" & vbCr
Loop

If nInput Then WScript.Echo "Selection:  " & nInput Else _
  WScript.Echo "Selection cancelled."

---

Joe Earnest



Sun, 31 Jul 2005 06:43:07 GMT  
 How to check input range - basic loop question
Thanks for the great help, I really appreciate it!  I will use both of your
suggestions!


Quote:
> Hi Again,

> Just noting that, also, you don't have a way to allow the user to cancel
> out.  If this is not intended, a cancel at an InputBox returns a null
string
> ( "" ).  A null string or a non-numeric input by the user will create a
> runtime error, if you use the CInt or forced conversions that I suggested.
> You may want error-trapping code.  Consider something like the following:

> ---

> nMin = 1
> nMax = 9

> Do
>   sInput= InputBox(sTryAgain _
>     & "Please input a number 1-9.", "Number Selection", sInput)
>   If (sInput="") Then nInput= 0: Exit Do
>   On Error Resume Next
>     nInput= CInt(Trim(sInput)): If Err Then nInput= 0
>   On Error GoTo 0
>   If NOT ((nInput<nMin) OR (nInput>nMax)) Then Exit Do
>   sTryAgain= "Invalid Input" & vbCr
> Loop

> If nInput Then WScript.Echo "Selection:  " & nInput Else _
>   WScript.Echo "Selection cancelled."

> ---

> Joe Earnest



Sun, 31 Jul 2005 22:19:24 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Question: How do you script to check basic performance of an app

2. How to: Loop through all objects and count the checked check boxes

3. check ip range

4. range.moveToElementText(range.parentElement())

5. Help: Looping a script until appropriate input.

6. Checking if PS File is correct from within a programming language like Visual Basic

7. Check the Status of a disk (Dynamic or Basic)

8. how to check a value in an array without looping

9. Check the Status of a disk (Dynamic or Basic)

10. Checking Input Box Types

11. how to check input on form submission?

12. VBScript to check CLSIDs/ProgIDs from list in input file

 

 
Powered by phpBB® Forum Software