'The following code illustrates how an input can be validated in VBScript. This example program demonstrates how to validate for a numeric value between 5 and 10 nInputNumber=inputbox("Enter a number between 5 and 10") 'Validate the input (if it is a valid number) bFlag=False Do While bFlag=False If isNumeric(nInputNumber) Then 'Valid number If nInputNumber<=10 Then 'Lesser than 10 If nInputNumber>=5 Then 'Greater than 5 bFlag=True Else bFlag=False nArrayBound=inputbox("Enter a number greater than or equal to 5") End If Else bFlag=False nArrayBound=inputbox("Enter a number Less than or equal to 10") End If Else bFlag=False nArrayBound=inputbox("Enter a valid number") End If Loop Msgbox "All Conditions Satisfied"
Program Explained:
The above code illustrates how an input can be validated in VBScript. This example program demonstrates how to validate for a numeric value between 5 and 10. I have explained the executable lines below:
Line 3: We use an Input box to get a value from the user and store it in a variable nInputNumber
Line 7: We set the bFlag variable as false, this variable is a falgging boolean varible which will be set as true when our conditions are satisfied.
Line 9: (Do Loop) Looping the statements until our conditions are satisfied (Numeric Value & greater than or equal to 5 & less than or equal to 10)
Line 11: (If isNumeric(nInputNumber)) Checking for condition 1 - Is it a number?
Line 13: (If nInputNumber<=10) Checking for condition 2 - Is it Less than or equal to 10?
Line 11: (If nInputNumber<=10) Checking for condition 1 - Is it Greater than or equal to 5?
Lines 16,18,24,30: Setting the bFlag (Flag) variable as True/False according to whether or not the condition is satisfied.
Lines 19,25,31: Throwing of appropriate error message back to the user and getting a new input from the user
Line 36: Message box confirming that it is out of loop and all the conditions are satisfied.
More VBScipt examples here.
hi everyone,
ReplyDeleteThe above program is very good bt a bit complicated. We can validate a user input using a very simple code as given below :
n=inputbox("enter a number between 5 and 10","Input Validation")
if n<5 OR n>10 then
msgbox "invalid i/p",,"Input Validation"
else
msgbox "valid input",,"Input Validation"
end if
Thanks
oh very nice
ReplyDeleteHi Admin,
ReplyDeleteThanks for useful code to learn vb-scripts
In the above code, inside if condition you have given different variable name, it impact looping condition.
Thanks