<html>
<head>
<script language="vbscript">
sub GetUserName()
dim Value
Value=form.txtnumber.value
if (Not IsNumeric(Value)) then
msgbox "You typed none-numeric value,
please type number between 1 and 5",vbexclamation
exit sub
end if
select case Value
case 0
msgbox "You typed 0"
case 1
msgbox"You typed
"&Value
case 2
msgbox "You typed 2"
case 3
msgbox "You typed
"&Value
case 4
msgbox"You typed 4"
case 5
msgbox "You typed 5"
case else
msgbox "Wrong number, type 1 to
5 numbers please.",vbexclamation
end select
end sub
</script>
</head>
<body>
<form name="form">
<input type="text" name="txtnumber" size="2">
Type a number between 0 and 5 inclusive<br>
<input type="button" value="Click"
onclick="call GetUserName()">
</form>
</body>
</html>
| Explanations: We created a form
with text box, and button. Then we created the variable
Value and set to the value of the text box. This is simply done
to avoid repeating long statement for txtnumber value. We checked if the value entered in the text box is not a number
if(Not IsNumeric(Value)).
Not means reject and IsNumeric checks if
the value is numeric. A message informs back if the value
is not a number. Notice that we use both hard coded value and variable to
show both ways. We defined select case statement using our variable
Case 1 means if the user enters number 1. We display
the typed number on a message box. Notice that vbexclamation display an
exclamation mark on the left of the message. This is one of the pre-defined vb script
functions. The default is vbokonly, which display ok button only. You can specify, YES, NO, CANCEL or
combination of any or all. Case else is anything other then our
conditions. In this case, case else is any number greater 5 or less than
0. See the result:
|