What is VBScript?
VBScript is a Microsoft scripting language.
VBScript is a scripting language .A scripting language is a
lightweight programming language .VBScript is a light version of Microsoft's
programming language Visual Basic . Add VBScript to your HTML pages, to make
your web site more dynamic and interactive.
How Does it Work?
When a VBScript is inserted into a HTML document, the Internet
browser will read the HTML and interpret the VBScript. The VBScript can be
executed immediately, or at a later event.
How to Put VBScript Code in an HTML Document
<html>
<head>
</head>
<body>
<script type="text/vbscript">
document.write("Hello from VBScript!")
</script>
</body>
</html>
And it produces this output:
Hello from VBScript!
To insert a script in an HTML document, use the <script>
tag. Use the type attribute to define the scripting language.
<script type="text/vbscript">
Then comes the VBScript: The command for writing some text on a
page is document.write:
document.write("Hello from VBScript!")
The script ends:
</script>
How to Handle Older Browsers
Older browsers that do not support scripts will display the script
as page content. To prevent them from doing this, you can use the HTML comment
tag:
<script type="text/vbscript">
<!--
some statements
-->
</script>
Example 1 :
<html>
<body>
<script type="text/vbscript">
document.write("Hello from VBScript!")
</script>
</body>
</html>
Output : Hello
from VBScript!
Example 2 :
<html>
<body>
<script type="text/vbscript">
document.write("<h1>Hello
World!</h1>")
document.write("<h2>Hello
World!</h2>")
</script>
</body>
</html>
Output :
Hello World!
Hello World!
Where to Put the VBScript
Examples
Head section
Scripts can be placed in the head section. Usually we put all the "functions" in the head section. The reason for this is to be sure that the script is loaded before the function is called.
Scripts can be placed in the head section. Usually we put all the "functions" in the head section. The reason for this is to be sure that the script is loaded before the function is called.
Body section
Execute a script that is placed in the body section. Scripts in the body section are executed when the page is loading.
Execute a script that is placed in the body section. Scripts in the body section are executed when the page is loading.
Scripts in a page will be executed immediately while the page loads
into the browser. This is not always what we want. Sometimes we want to execute
a script when a page loads, other times when a user triggers an event.
Scripts in the head section: Scripts to be executed when they are called or when an
event is triggered go in the head section. When you place a script in the head
section you will assure that the script is loaded before anyone uses it:
<html>
<head>
<script type="text/vbscript">
some statements
</script>
</head>
Scripts in the body section: Scripts to be executed when the page loads go in the body
section. When you place a script in the body section it generates the content
of the page:
<html>
<head>
</head>
<body>
<script type="text/vbscript">
some statements
</script>
</body>
Scripts in both the body and the head section: You can place an unlimited number of
scripts in your document, so you can have scripts in both the body and the head
section.
<html>
<head>
<script type="text/vbscript">
some statements
</script>
</head>
<body>
<script type="text/vbscript">
some statements
</script>
</body>
Create a variable
Variables are used to store information. This example demonstrates how you can create a variable, and assign a value to it.
Variables are used to store information. This example demonstrates how you can create a variable, and assign a value to it.
<html>
<body>
<script type="text/vbscript">
dim name
name="Jan Egil"
document.write(name)
</script>
</body>
</html>
Jan Egil
Insert a variable value in a text
This example demonstrates how you can insert a variable value in a text.
This example demonstrates how you can insert a variable value in a text.
<html>
<body>
<script type="text/vbscript">
dim name
name="Jan Egil"
document.write("My name is: " & name)
</script>
</body>
</html>
My name is: Jan Egil
Create an array
Arrays are used to store a series of related data items. This example demonstrates how you can make an array that stores names. ( We are using a "for loop" to demonstrate how you write the names. )
Arrays are used to store a series of related data items. This example demonstrates how you can make an array that stores names. ( We are using a "for loop" to demonstrate how you write the names. )
<html>
<body>
<script type="text/vbscript">
dim famname(5)
famname(0)="Jan Egil"
famname(1)="Tove"
famname(2)="Hege"
famname(3)="Stale"
famname(4)="Kai Jim"
famname(5)="Borge"
for i=0 to 5
document.write(famname(i)
& "<br />")
next
</script>
Jan Egil
Tove
Hege
Stale
Kai Jim
Borge
Tove
Hege
Stale
Kai Jim
Borge
What is a Variable?
A variable is a "container" for information you want to
store. A variable's value can change during the script. You can refer to a variable
by name to see its value or to change its value. In VBScript, all variables are
of type variant, that can store different types of data.
Rules for Variable Names:
- Must begin with a letter
- Cannot contain a period (.)
- Cannot exceed 255 characters
Declaring Variables
You can declare variables with the Dim, Public or the Private
statement. Like this:
dim name
name=some value
Now you have created a variable. The name of the variable is
"name".
You can also declare variables by using its name in your script.
Like this:
name=some value
Now you have also created a variable. The name of the variable is
"name".
However, the last method is not a good practice, because you can
misspell the variable name later in your script, and that can cause strange
results when your script is running. This is because when you misspell for
example the "name" variable to "nime" the script will
automatically create a new variable called "nime". To prevent
your script from doing this you can use the Option Explicit statement. When you
use this statement you will have to declare all your variables with the dim,
public or private statement. Put the Option Explicit statement on the top of
your script. Like this:
option explicit
dim name
name=some value
Assigning Values to Variables
You assign a value to a variable like this:
name="Hege"
i=200
The variable name is on the left side of the expression and the
value you want to assign to the variable is on the right. Now the variable
"name" has the value "Hege".
Lifetime of Variables
How long a variable exists is its lifetime.
When you declare a variable within a procedure, the variable can only be accessed within that procedure. When the procedure exits, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared.
If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.
When you declare a variable within a procedure, the variable can only be accessed within that procedure. When the procedure exits, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared.
If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.
Array Variables
Sometimes you want to assign more than one value to a single
variable. Then you can create a variable that can contain a series of values.
This is called an array variable. The declaration of an array variable uses
parentheses ( ) following the variable name. In the following example, an array
containing 3 elements is declared:
dim names(2)
The number shown in the parentheses is 2. We start at zero so this
array contains 3 elements. This is a fixed-size array. You assign data to each
of the elements of the array like this:
names(0)="Tove"
names(1)="Jani"
names(2)="Stale"
Similarly, the data can be retrieved from any element using the
index of the particular array element you want. Like this:
mother=names(0)
You can have up to 60 dimensions in an array. Multiple dimensions
are declared by separating the numbers in the parentheses with commas. Here we
have a two-dimensional array consisting of 5 rows and 7 columns:
dim table(4, 6)
Examples
Sub procedure
The sub procedure does not return a value.
The sub procedure does not return a value.
<html>
<head>
<script type="text/vbscript">
sub mySub()
msgbox("This is a sub
procedure")
end sub
</script>
</head>
<body>
<script type="text/vbscript">
call mySub()
</script>
<p>A sub procedure does not return a result.</p>
</body>
</html>
A sub procedure does not return a result.
Function procedure
The function procedure is used if you want to return a value.
The function procedure is used if you want to return a value.
<html>
<head>
<script type="text/vbscript">
function myFunction()
myFunction =
"BLUE"
end function
</script>
</head>
<body>
<script type="text/vbscript">
document.write("My favorite color is " &
myFunction())
</script>
<p>A function procedure CAN return a result.</p>
</body>
</html>
My favorite color is BLUE
A function procedure CAN return a result.VBScript Procedures
We have two kinds of procedures: The Sub procedure and the
Function procedure.
A Sub procedure:
- is a series of statements, enclosed by the Sub and End Sub statements
- can perform actions, but does not return a value
- can take arguments that are passed to it by a calling procedure
- without arguments, must include an empty set of parentheses ()
Sub mysub()
some statements
End Sub
or
Sub mysub(argument1,argument2)
some statements
End Sub
A Function procedure:
- is a series of statements, enclosed by the Function and End Function statements
- can perform actions and can return a value
- can take arguments that are passed to it by a calling procedure
- without arguments, must include an empty set of parentheses ()
- returns a value by assigning a value to its name
Function myfunction()
some statements
myfunction=some value
End Function
or
Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function
Function myfunction()
some statements
myfunction=some value
End Function
or
Function myfunction(argument1,argument2)
some statements
myfunction=some value
End Function
Call a Sub or Function Procedure
When you call a Function in your code, you do like this:
name = findname()
Here you call a Function called "findname", the Function
returns a value that will be stored in the variable "name".
Or, you can do like this:
msgbox "Your name is " & findname()
Here you also call a Function called "findname", the
Function returns a value that will be displayed in the message box.
When you call a Sub procedure you can use the Call statement, like
this:
Call MyProc(argument)
Or, you can omit the Call statement, like this:
MyProc argument
Conditional Statements
Very often when you write code, you want to perform different
actions for different decisions. You can use conditional statements in your
code to do this.
In VBScript we have three conditional statements:
The if...then...else statement
This example demonstrates how to write the if...then..else
statement.
<html>
<head>
<script type="text/vbscript">
function greeting()
i=hour(time)
if i < 10 then
document.write("Good
morning!")
else
document.write("Have a
nice day!")
end if
end function
</script>
</head>
<body onload="greeting()">
</body>
</html>
Have a nice day!
The if...then...elseif... statement
This example demonstrates how to write the if...then...elseif statement.
This example demonstrates how to write the if...then...elseif statement.
<html>
<head>
<script type="text/vbscript">
function greeting()
i=hour(time)
If i = 10 then
document.write("Just
started...!")
elseif i = 11 then
document.write("Hungry!")
elseif i = 12 then
document.write("Ah,
lunch-time!")
elseif i = 16 then
document.write("Time
to go home!")
else
document.write("Unknown")
end if
end function
</script>
</head>
<body onload="greeting()">
</body>
</html>
Unknown
The select case statement
This example demonstrates how to write the select case statement.
This example demonstrates how to write the select case statement.
<html>
<body>
<script type="text/vbscript">
d=weekday(date)
select case d
case 1
document.write("Sleepy Sunday")
case 2
document.write("Monday again!")
case 3
document.write("Just Tuesday!")
case 4
document.write("Wednesday!")
case 5
document.write("Thursday...")
case 6
document.write("Finally Friday!")
case else
document.write("Super
Saturday!!!!")
end select
</script>
<p>This example demonstrates the "select case"
statement.<br />
You will receive a different greeting based on what day it
is.<br />
Note that Sunday=1, Monday=2, Tuesday=3, etc.</p>
</body>
</html>
Finally Friday!
This example demonstrates the "select case" statement.You will receive a different greeting based on what day it is.
Note that Sunday=1, Monday=2, Tuesday=3, etc
- if statement - use this statement if you want to execute a set of code when a condition is true
- if...then...else statement - use this statement if you want to select one of two sets of lines to execute
- if...then...elseif statement - use this statement if you want to select one of many sets of lines to execute
- select case statement - use this statement if you want to select one of many sets of lines to execute
If....Then.....Else
You should use the If...Then...Else statement if you want to
- execute some code if a condition is true
- select one of two blocks of code to execute
If you want to execute only one statement when a condition
is true, you can write the code on one line:
if i=10 Then msgbox "Hello"
There is no ..else.. in this syntax. You just tell the code to
perform one action if the condition is true (in this case if i=10).
If you want to execute more than one statement when a
condition is true, you must put each statement on separate lines and end the
statement with the keyword "End If":
if i=10 Then
msgbox "Hello"
i = i+1
end If
There is no ..else.. in this syntax either. You just tell the code
to perform multiple actions if the condition is true.
If you want to execute a statement if a condition is true and
execute another statement if the condition is not true, you must add the
"Else" keyword:
if i=10 then
msgbox "Hello"
else
msgbox "Goodbye"
end If
The first block of code will be executed if the condition is true,
and the other block will be executed otherwise (if i is not equal to 10).
If....Then.....Elseif
You can use the if...then...elseif statement if you want to select
one of many blocks of code to execute:
if payment="Cash" then
msgbox "You are going to pay cash!"
elseif payment="Visa" then
msgbox "You are going to pay with visa."
elseif payment="AmEx" then
msgbox "You are going to pay with American Express."
else
msgbox "Unknown method of payment."
end If
Select Case
You can also use the SELECT statement if you want to select one of
many blocks of code to execute:
select case payment
case "Cash"
msgbox "You are going to pay cash"
case "Visa"
msgbox "You are going to pay with visa"
case "AmEx"
msgbox "You are going to pay with American Express"
case Else
msgbox "Unknown method of payment"
end select
This is how it works: First we have a single expression (most
often a variable), that is evaluated once. The value of the expression is then
compared with the values for each Case in the structure. If there is a match,
the block of code associated with that Case is executed.
Looping Statements
Very often when you write code, you want to allow the same block
of code to run a number of times. You can use looping statements in your code
to do this.
In VBScript we have four looping statements:
- For...Next statement - runs statements a specified number of times.
- For Each...Next statement - runs statements for each item in a collection or each element of an array
- Do...Loop statement - loops while or until a condition is true
- While...Wend statement - Do not use it - use the Do...Loop statement instead
Examples
For...next loop
This example demonstrates how to make a simple For....Next loop.
<html>
<body>
<script type="text/vbscript">
for i = 0 to 5
document.write("The
number is " & i & "<br />")
next
</script>
</body>
</html>
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Looping through headers
This example demonstrates how you can loop through the 6 headers in html.
This example demonstrates how you can loop through the 6 headers in html.
<html>
<body>
<script type="text/vbscript">
for i=1 to 6
document.write("<h" & i &
">This is header " & i & "</h" & i &
">")
next
</script>
</body>
</html>
This is header 1
This is header 2
This is header 3
This is header 4
This is header 5
This is header 6
For...each loop
This example demonstrates how to make a simple For.....Each loop.
This example demonstrates how to make a simple For.....Each loop.
<html>
<body>
<script type="text/vbscript">
dim names(2)
names(0) = "Tove"
names(1) = "Jani"
names(2) = "Hege"
for each x in names
document.write(x &
"<br />")
next
</script>
</body>
</html>
Tove
Jani
Hege
Jani
Hege
Do...While loop
This example demonstrates how to make a simple Do...While loop.
This example demonstrates how to make a simple Do...While loop.
<html>
<body>
<script type="text/vbscript">
i=0
do while i < 10
document.write(i &
"<br />")
i=i+1
loop
</script>
</body>
</html>
0
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
For...Next
You can use a For...Next statement to run a block of code,
when you know how many repetitions you want.
You can use a counter variable that increases or decreases with
each repetition of the loop, like this:
For i=1 to 10
some code
Next
The For statement specifies the counter variable (i)
and its start and end values. The Next statement increases the counter
variable (i) by one.
Step Keyword
Using the Step keyword, you can increase or decrease the
counter variable by the value you specify.
In the example below, the counter variable (i) is increased
by two each time the loop repeats.
For i=2 To 10 Step 2
some code
Next
To decrease the counter variable, you must use a negative Step
value. You must specify an end value that is less than the start value.
In the example below, the counter variable (i) is decreased
by two each time the loop repeats.
For i=10 To 2 Step -2
some code
Next
Exit a For...Next
You can exit a For...Next statement with the Exit For keyword.
For Each...Next
A For Each...Next loop repeats a block of code for each
item in a collection, or for each element of an array.
The For Each...Next statement looks almost identical to the
For...Next statement. The difference is that you do not have to specify the
number of items you want to loop through.
dim names(2)
names(0)="Tove"
names(1)="Jani"
names(2)="Hege"
For Each x in names
document.write(x & "<br />")
Next
Do...Loop
You can use Do...Loop statements to run a block of code when you
do not know how many repetitions you want. The block of code is repeated while
a condition is true or until a condition becomes true.
Repeating Code While a Condition is True
You use the While keyword to check a condition in a Do...Loop
statement.
Do While i>10
some code
Loop
If i equals 9, the code inside the loop above will never be
executed.
Do
some code
Loop While i>10
The code inside this loop will be executed at least one time, even
if i is less than 10.
Repeating Code Until a Condition Becomes True
You use the Until keyword to check a condition in a Do...Loop
statement.
Do Until i=10
some code
Loop
If i equals 10, the code inside the loop will never be
executed.
Do
some code
Loop Until i=10
The code inside this loop will be executed at least one time, even
if i is equal to 10.
Exit a Do...Loop
You can exit a Do...Loop statement with the Exit Do keyword.
Do Until i=10
i=i-1
If i<10 Then Exit Do
Loop
The code inside this loop will be executed as long as i is
different from 10, and as long as i is greater than 10.
Inbuilt functions
Date/Time Functions
Function
|
Description
|
Converts a valid date and time expression to the variant of
subtype Date
|
|
Returns the current system date
|
|
Returns a date to which a specified time interval has been added
|
|
Returns the number of intervals between two dates
|
|
Returns the specified part of a given date
|
|
Returns the date for a specified year, month, and day
|
|
Returns a date
|
|
Returns a number that represents the day of the month (between 1
and 31, inclusive)
|
|
Returns an expression formatted as a date or time
|
|
Returns a number that represents the hour of the day (between 0
and 23, inclusive)
|
|
Returns a Boolean value that indicates if the evaluated
expression can be converted to a date
|
|
Returns a number that represents the minute of the hour (between
0 and 59, inclusive)
|
|
Returns a number that represents the month of the year (between
1 and 12, inclusive)
|
|
Returns the name of a specified month
|
|
Returns the current system date and time
|
|
Returns a number that represents the second of the minute
(between 0 and 59, inclusive)
|
|
Returns the current system time
|
|
Returns the number of seconds since 12:00 AM
|
|
Returns the time for a specific hour, minute, and second
|
|
Returns a time
|
|
Returns a number that represents the day of the week (between 1
and 7, inclusive)
|
|
Returns the weekday name of a specified day of the week
|
|
Returns a number that represents the year
|
Conversion Functions |
|
Function
|
Description
|
Converts the first letter in a string to ANSI code
|
|
Converts an expression to a variant of subtype Boolean
|
|
Converts an expression to a variant of subtype Byte
|
|
Converts an expression to a variant of subtype Currency
|
|
Converts a valid date and time expression to the variant of
subtype Date
|
|
Converts an expression to a variant of subtype Double
|
|
Converts the specified ANSI code to a character
|
|
Converts an expression to a variant of subtype Integer
|
|
Converts an expression to a variant of subtype Long
|
|
Converts an expression to a variant of subtype Single
|
|
Converts an expression to a variant of subtype String
|
|
Returns the hexadecimal value of a specified number
|
|
Returns the octal value of a specified number
|
Format Functions |
|
Function
|
Description
|
Returns an expression formatted as a currency value
|
|
Returns an expression formatted as a date or time
|
|
Returns an expression formatted as a number
|
|
Returns an expression formatted as a percentage
|
Math Functions |
|
Function
|
Description
|
Returns the absolute value of a specified number
|
|
Returns the arctangent of a specified number
|
|
Returns the cosine of a specified number (angle)
|
|
Returns e raised to a power
|
|
Returns the hexadecimal value of a specified number
|
|
Returns the integer part of a specified number
|
|
Returns the integer part of a specified number
|
|
Returns the natural logarithm of a specified number
|
|
Returns the octal value of a specified number
|
|
Returns a random number less than 1 but greater or equal to 0
|
|
Returns an integer that indicates the sign of a specified number
|
|
Returns the sine of a specified number (angle)
|
|
Returns the square root of a specified number
|
|
Returns the tangent of a specified number (angle)
|
Array Functions |
|
Function
|
Description
|
Returns a variant containing an array
|
|
Returns a zero-based array that contains a subset of a string
array based on a filter criteria
|
|
Returns a Boolean value that indicates whether a specified
variable is an array
|
|
Returns a string that consists of a number of substrings in an
array
|
|
Returns the smallest subscript for the indicated dimension of an
array
|
|
Returns a zero-based, one-dimensional array that contains a
specified number of substrings
|
|
Returns the largest subscript for the indicated dimension of an
array
|
String Functions |
|
Function
|
Description
|
Returns the position of the first occurrence of one string
within another. The search begins at the first character of the string
|
|
Returns the position of the first occurrence of one string
within another. The search begins at the last character of the string
|
|
Converts a specified string to lowercase
|
|
Returns a specified number of characters from the left side of a
string
|
|
Returns the number of characters in a string
|
|
Removes spaces on the left side of a string
|
|
Removes spaces on the right side of a string
|
|
Removes spaces on both the left and the right side of a string
|
|
Returns a specified number of characters from a string
|
|
Replaces a specified part of a string with another string a
specified number of times
|
|
Returns a specified number of characters from the right side of
a string
|
|
Returns a string that consists of a specified number of spaces
|
|
Compares two strings and returns a value that represents the
result of the comparison
|
|
Returns a string that contains a repeating character of a
specified length
|
|
Reverses a string
|
|
Converts a specified string to uppercase
|
Other Functions |
|
Function
|
Description
|
Creates an object of a specified type
|
|
Eval
|
Evaluates an expression and returns the result
|
Returns the current locale ID
|
|
Returns a reference to an automation object from a file
|
|
Allows you to connect a VBScript procedure to a DHTML event on
your pages
|
|
Displays a dialog box, where the user can write some input
and/or click on a button, and returns the contents
|
|
Returns a Boolean value that indicates whether a specified
variable has been initialized or not
|
|
Returns a Boolean value that indicates whether a specified
expression contains no valid data (Null)
|
|
Returns a Boolean value that indicates whether a specified
expression can be evaluated as a number
|
|
Returns a Boolean value that indicates whether the specified
expression is an automation object
|
|
Returns a picture object. Available only on 32-bit platforms
|
|
Displays a message box, waits for the user to click a button,
and returns a value that indicates which button the user clicked
|
|
Returns a number that represents an RGB color value
|
|
Rounds a number
|
|
Returns the scripting language in use
|
|
Returns the build version number of the scripting engine in use
|
|
Returns the major version number of the scripting engine in use
|
|
Returns the minor version number of the scripting engine in use
|
|
Sets the locale ID and returns the previous locale ID
|
|
Returns the subtype of a specified variable
|
|
Returns a value that indicates the subtype of a specified
variable
|
VBScript Keywords
Keyword
|
Description
|
Empty
|
Used to indicate an
uninitialized variable value. A variable value is uninitialized when it is
first created and no value is assigned to it, or when a variable value is
explicitly set to empty. dim x 'the variable x is uninitialized! x="ff" 'the variable x is NOT uninitialized anymore x=empty 'the variable x is uninitialized! Note: This is not the same as Null!! |
False
|
Has a value equal to 0
|
Nothing
|
Used to disassociate an object variable from an object to
release system resources.
Example: set myObject=Nothing |
Null
|
Used to indicate that a variable contains no valid data.
Note: This is not the same as Empty!! |
True
|
Has a value equal to -1
|
Example : 1
<html>
<body>
<script type="text/vbscript">
document.write("Today's date is " & date())
document.write("<br />")
document.write("The time is " & time())
</script>
</body>
</html>
Today's date is 1/13/06
The time is 9:02:20 PM
The time is 9:02:20 PM
Example : 2
<html>
<body>
<p>VBScripts' function <b>WeekdayName</b> is
used to get a weekday:</p>
<script type="text/vbscript">
document.write("<p>")
document.write(WeekDayName(1))
document.write("<br />")
document.write(WeekDayName(2))
document.write("</p><p>")
document.write("Get the abbreviated name of a weekday:")
document.write("<br />")
document.write(WeekDayName(1,true))
document.write("<br />")
document.write(WeekDayName(2,true))
document.write("</p><p>")
document.write("Get the current weekday:")
document.write("<br />")
document.write(WeekdayName(weekday(date)))
document.write("<br />")
document.write(WeekdayName(weekday(date), true))
document.write("</p>")
</script>
</body>
</html>
VBScripts' function WeekdayName is used to get a weekday:Sunday
Monday
Get the abbreviated name of a weekday:
Sun
Mon
Get the current weekday:
Friday
Fri
Example : 3
<html>
<body>
<p>VBScripts' function <b>MonthName</b> is used
to get a month:</p>
<script type="text/vbscript">
document.write("<p>")
document.write(MonthName(1))
document.write("<br />")
document.write(MonthName(2))
document.write("</p><p>")
document.write("Here is how you get the abbreviated name of a
month:")
document.write("<br />")
document.write(MonthName(1,true))
document.write("<br />")
document.write(MonthName(2,true))
document.write("</p><p>")
document.write("Here is how you get the current month:")
document.write("<br />")
document.write(MonthName(month(date)))
document.write("<br />")
document.write(MonthName(month(date), true))
document.write("</p>")
</script>
</body>
</html>
January
February
Here is how you get the abbreviated name of a month:
Jan
Feb
Here is how you get the current month:
January
Jan
Example : 4
body>
<script type="text/vbscript">
sometext="Welcome to our Web Site!!"
document.write(Left(sometext,5))
document.write("<br />")
document.write(Right(sometext,5))
</script>
</body>
</html>
Welco
ite!!
ite!!
Example : 5
<html>
<body>
<script type="text/vbscript">
sometext="Welcome to our Web Site!!"
document.write(Mid(sometext, 9, 2))
</script>
</body>
</html>
to
Example : 7
<html>
<body>
<script type="text/vbscript">
sometext="Welcome to this Web!!"
document.write(Replace(sometext, "Web",
"Page"))
</script>
</body>
</html>
Welcome to this Page!!
Example : 7
<html>
<body>
<script type="text/vbscript">
txt="Have a nice day!"
document.write(ucase(txt))
document.write("<br />")
document.write(lcase(txt))
</script>
</body>
</html>
HAVE A NICE DAY!
have a nice day!
have a nice day!
Using VBScript with Forms
As the popularity of web
page forms increase, so does the need to be able to validate data before the
client browser submits it to the web server. As a scripting language, VBScript
is well suited for this task. Once the form has been validated, the same script
can be used to forward the data on to the server. We will look at both the
process of validating and submitting forms.
Validating Your Forms
The process of validating forms involves checking the form to see
if:
·
All
of the required data is proved
·
The
data provided is valid
Meticulous data validation scripts can be tedious to code but are
well worth their return in verifying the quality of the data.
The validation example that we will be examining does not contain
anything new in the way of VBScript. We are simply using the elements that we
have learned previously in a new way. Before reading any further you may find
if beneficial to ponder how you would validate an HTML form using the VBScript
techniques that you have learned.
Checking Form Input
This example is pretty simple. It has a single field in which the
user can enter their age and a single command button that is used to submit
their age to the server. A copy of this example can be found in exam_5a.htm.
<HTML>
<HEAD>
<TITLE>Working With VBScript: Example 5a</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!-- Instruct non-IE browsers to skip over VBScript modules.
Option Explicit
Sub cmdSubmit_OnClick
' Check to see if the user entered anything.
If (Len(document.frmExample5a.txtAge.value) = 0) Then
MsgBox "You must enter your age before submitting."
Exit Sub
End If
' Check to see if the user entered a number.
If (Not(IsNumeric(document.frmExample5a.txtAge.value))) Then
MsgBox "You must enter a number for your age."
Exit Sub
End If
' Check to see if the age entered is valid.
If (document.frmExample5a.txtAge.value < 0) Or _
(document.frmExample5a.txtAge.value > 100) Then
MsgBox "The age you entered is invalid."
Exit Sub
End If
' Data looks okay so submit it.
MsgBox "Thanks for providing your age."
document.frmExample5a.submit
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<H1>A VBScript Example on Variables</H1>
<P> This example demonstrates validation techniques in VBScript. </P>
<FORM NAME="frmExample5a">
<TABLE>
<TR>
<TD>Enter your age:</TD>
<TD><INPUT TYPE="Text" NAME="txtAge" SIZE="2">
<TR>
<TD><INPUT TYPE="Button" NAME="cmdSubmit" VALUE="Submit"></TD>
<TD></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
How It Works
The heart of this validation script is found in the click event
procedure for the cmdSubmit command button. We start by checking if the
user entered anything at all into the field using VBScript's Len
function. This function returns the length of a string. If the length is 0, the
data is invalid. We inform the user and exit the submit procedure via the Exit
Sub statement:
' Check to see if the user entered anything.
If (Len(document.frmExample5a.txtAge.value) = 0) Then
MsgBox "You must enter your age before submitting."
Exit Sub
End If
Next we check to see if what the user entered is a numeric value.
The VBScript function IsNumeric returns a true value when it is a
number. If not, we tell the user and exit:
' Check to see if the user entered a number.
If (Not(IsNumeric(document.frmExample5a.txtAge.value))) Then
MsgBox "You must enter a number for your age."
Exit Sub
End If
Our final check involves verifying that the age they entered seems
reasonable for our environment. I have determined that no age less than 0 or greater
than 100 is acceptable. Using an If..Then statement we can check the
value of the input field against this criteria:
' Check to see if the age entered is valid.
If (document.frmExample5a.txtAge.value < 0) Or _
(document.frmExample5a.txtAge.value > 100) Then
MsgBox "The age you entered is invalid."
Exit Sub
End If
That's it. While this example is by no means the most detailed
validation script you will encounter it provides you with a basis of what is
possible with VBScript.
Submitting Your Forms
Compared to validation, the process of submitting a form is
simple. In our example we've used a normal HTML button with the Submit
caption that is tied to an event procedure that both validates and at the same
time submits the form. we've demonstrated how to use function MyButton_onSubmit,
as an alternative.
The code that we would have to add to our previous example to
submit the form is shown below:
' Data looks okay so submit it.
MsgBox "Thanks for providing your age."
document.frmExample5a.submit
The MsgBox statement lets the user know that their data has
been processed. The form is then submitted by invoking the Submit method
of the form object. As we saw in lesson 3 on objects, methods cause an object
to perform a task. Here we are using the submit method of our form to
cause the form to submit its data, just as if we had used a submit
control.
___________________________________________________________________________________
0 comments :
Post a Comment