Original:
AbCdEfGhIjKlMnOpQrStUvWxYz
TheString="AbCdEfGhIjKlMnOpQrStUvWxYz"
LCase: <%
LCase:
=LCase("AbCdEfGhIjKlMnOpQrStUvWxYz") %>
abcdefghijklmnopqrstuvwxyz
UCase: <%
UCase:
=UCase("AbCdEfGhIjKlMnOpQrStUvWxYz") %>
ABCDEFGHIJKLMNOPQRSTUV
WXYZ
InStr(string, string1) Position of string1 within string. See also Compare strings
TheString="My cat is very nice"<% TheString="My
cat is very nice" %>
String: "My cat is very nice"
Location of cat within TheString?
InStr(TheString,"cat"): <% =InStr(TheString,"cat") %>
cat?
InStr(TheString,"cat"): 4
very?
InStr(TheString,"very"): <% =InStr(TheString,"very")
very?
%>
InStr(TheString,"very"): 11
nice?
nice?
InStr(TheString,"nice"): <% =InStr(TheString,"nice")
InStr(TheString,"nice"): 16
%>
dog?
dog?
InStr(TheString,"dog"): 0
InStr(TheString,"dog"): <% =InStr(TheString,"dog")
%>
CAT?
InStr(TheString,"CAT"): 0
CAT?
InStr(TheString,"CAT"): <% =InStr(TheString,"CAT")
%>
Len(string) and LenB(string) Length of string (number or character or bytes)
<pre> Number of characters in a string
Number of characters in a string
Len("0123456789"): 10
Len("0123456789"): <% =Len("0123456789") %> Len("My cat is very nice"): 19
Len("My cat is very nice"): <% =Len("My cat is very nice") %> Len("AbCdEfGhIjKlMnOpQrStUvWxYz")
Len("AbCdEfGhIjKlMnOpQrStUvWxYz"): <% : 26
=Len("AbCdEfGhIjKlMnOpQrStUvWxYz") %>
For numbers will indicate number of bytes
For numbers will indicate number of bytes required by store required by store number
number
Len (1): 1
Len (1): <% =Len(1) %> Len (10): 2
Len (10): <% =Len(10) %> Len (100): 3
Len (100): <% =Len(100) %> Len (100.01): 6
Len (100.01): <% =Len(100.01) %>
Number of bytes to store a string
Number of bytes to store a string
LenB("0123456789"): 20
LenB("0123456789"): <% =LenB("0123456789") %> LenB("My cat is very nice"): 38
LenB("My cat is very nice"): <% =LenB("My cat is very nice") LenB("AbCdEfGhIjKlMnOpQrStUvWxYz
%> "): 52
LenB("AbCdEfGhIjKlMnOpQrStUvWxYz"): <%
=LenB("AbCdEfGhIjKlMnOpQrStUvWxYz") %>
</pre>
Replace(string,string1,string2) Replace string1 by string2 within string
<pre>
String: "My cat is very nice" String: "My cat is very nice"
Abs(number)
Absolute value of a number Absolute value of a number
All available server variables may be obtained with the script bellow. Just save the
script to your server and it will show you the values for each of the
ServerVariables (use an url like
http://www.yoursite.com/yourdir/servervariables.asp?a=hello&b=end)
servervariables.asp
<html><head><TITLE>Server Variables</TITLE></head>
<body bgcolor=FFFFFF>
<%
For Each Key in Request.ServerVariables
response.write Key & ": " & request.servervariables(Key) & " <br><br>"
Next
%>
</body></html>
List of server variables
ALL_HTTP
ALL_RAW
APPL_MD_PATH
APPL_PHYSICAL_PATH
AUTH_PASSWORD
AUTH_TYPE
AUTH_USER
CERT_COOKIE
CERT_FLAGS
CERT_ISSUER
CERT_KEYSIZE
CERT_SECRETKEYSIZE
CERT_SERIALNUMBER
CERT_SERVER_ISSUER
CERT_SERVER_SUBJECT
CERT_SUBJECT
CONTENT_LENGTH
CONTENT_TYPE
GATEWAY_INTERFACE
HTTPS
HTTPS_KEYSIZE
HTTPS_SECRET_KEYSIZE
HTTPS_SERVER_ISSUER
HTTPS_SERVER_SUBJECT
INSTANCE_ID
INSTANCE_META_PATH
LOCAL_ADDR
LOGON_USER
PATH_INFO
PATH_TRANSLATED
QUERY_STRING
REMOTE_ADDR
REMOTE_HOST
REMOTE_USER
REQUEST_METHOD
SCRIPT_NAME
SERVER_NAME
SERVER_PORT
SERVER_PORT_SECURE
SERVER_PROTOCOL
SERVER_SOFTWARE
URL
HTTP_PRAGMA
HTTP_HOST
HTTP_ACCEPT_ENCODING
HTTP_ACCEPT_LANGUAGE See Language
specific response
HTTP_ACCEPT_CHARSET
HTTP_COOKIE
HTTP_VIA
HTTP_X_FORWARDED_FOR
HTTP_CACHE_CONTROL
HTTP_ACCEPT
HTTP_USER_AGENT
HTTP_REFERER
ASP_VERSION
ASP_VERSION_MAJOR
ASP_VERSION_MINOR
ASP_OS
Note: Even though "=date" is a short script, it's actually made up of two parts. The
"date" part tells the server, "Get me the date." The equal sign (=) tells the server to
display the date in the Web page. If you typed just:
<% date %>
the server would get the current date from your system, but that's all. It wouldn't
display it. There are times when it makes sense to use an ASP function without
the equal sign.
Time
To display the current time by itself, type:
<% =time %>
where you want it to appear. When you view the page, you should see something
like this:
4:19:46 PM
1/23/97 4:19:46 PM
Day
To display the day of the current month, type:
<% =day(now) %>
where you want it to appear. When you view the page, you'll see a number
between 1 and 31.
Year
To display the current year, type:
<% =year(now) %>
where you want it to appear.
Example
Suppose you wanted to display today's date as day/month/year instead
of month/day/year. To do so, you would use the day, month, and year ASP
functions together, by typing:
<% =day(now) %>/<% =month(now) %>/<% =year(now) %>
When you viewed the page, you would see something like this:
23/1/1997
Later we'll see how you can change this so only the last two digits of the year are
displayed, like this:
23/1/97
Example
Try typing this into a Web page:
The time is <% =time %>. That means it's <% =minute(now) %>
minutes past <% =hour(now) %> o'clock.
When you view the page in Internet Explorer, you should see something like this:
The time is 1:36:05 PM. That means it's 36 minutes past 13 o'clock.
Remember, the hour function is based on a 24-hour clock. Later we'll see how to
convert from the 24-hour clock to a 12-hour clock.
Timevalue
You probably won't ever use the timevalue function. It takes the different ways
you can write the time, such as "2:24PM" and "14:24," and returns them in this
format: "2:24:00 PM." This can be useful if you're using a function that needs to
be given the time in that exact format.
Example
Earlier in this section we saw how you can use the hour, minute, and second
functions to break up the time into hours, minutes, and seconds. With
the timevalue function, you can put them back together. Type this into a Web
page:
When it's 23 minutes and 5 seconds past 4 o'clock in the afternoon,
that means it's <% =timevalue("16:23:05") %>.
This is the same as <% =timevalue("4:23:05PM") %>
or <% =timevalue("16:23:05PM") %>.
Make sure you type "16:23:05PM" and not "16:23:05 PM." The "05" and the
"PM." should be run together, not separated by a space. When you view the page
in Internet Explorer, you should see:
When it's 23 minutes and 5 seconds past 4 o'clock in the afternoon, that means it's
4:23:05 PM. This is the same as 4:23:05 PM or 4:23:05 PM.
Displaying Text
len
The len function tells you how many characters are in a word or sequence of
words. (The name "len" is an abbreviation of "length.") All characters are counted,
including the space character. For example, to find the length of the sentence
"The cat is on the mat," type this into a Web page:
There are <% =len("The cat is on the mat.") %> characters in
"The cat is on the mat."
When you view the page in Internet Explorer, you should see this:
left
You can use the left function to look at the first few characters of a word or
sequence of words. For example, to find the first character of "Frankenstein,"
type this into a Web page:
"Frankenstein" begins with the letter <% =left("Frankenstein", 1) %>.
When you view the page, you should see this:
right
To look at the last few characters of a word or sequence of words, use
the right function. For example, to find the last three letters of "Wednesday,"
type this into a Web page:
The last three letters of "Wednesday" are: <% =right("Wednesday", 3) %>.
When you view this page, you should see this:
Example
What if you wanted to take a few letters from the middle of something? How
would you specify exactly where in the middle you wanted to be? For example,
how would you take out just the "apple" part of the word "pineapples"?
You could start with the fifth character from the left and then stop at the second
character from the right. Or you could do it the following way.
When you view this page in Internet Explorer, you should see this:
apples apple
pineapple apple
23/1/1997
We can also change the date display so only the last two digits of the year are
included. To do this, type
<% =day(now) %>/<% =month(now) %>/<% =Right((year(now)), 2) %>
Now when you view the page, you should see something like this:
23/1/97
In this example, we'll see how to change 24-hour clock times such as "13 o'clock"
to 12-hour clock times ("1 o'clock PM"). To do this, we'll need to make the server-
side script that uses the hour function a little more complicated. Instead of
<% =hour(now) %> o'clock
we'll need to write a script that looks at the hour and does one of the following:
With forms, there are two steps: first you create the form, and then you process it.
To create a form for an Active Server Page, just create a standard HTML form.
To try out this example, create an HTML file ("form_response.html") and cut-and-
paste the following text into it.
form_response.html
<html>
<head><title>Asking for information</title></head>
<body>
<form method="post" action="form_response.asp">
Your name: <input type="text" name="name" size="20"><BR>
Your email: <input type="password" name="email" size="15"><BR>
<input type="Submit" value="Submit">
</form>
</body>
</html>
Active Server Pages provide a mechanism for processing forms that, unlike CGI
scripting, doesn't involve serious programming: the Request.Form.
Considering the form above, we may create the file bellow and get a response.
form_response.asp
<html>
<head><title>Responding to a form</title></head>
<body>
Your name is <% =Request.Form("name") %> <BR>
Your email is <% =Request.Form("email") %>
</body>
</html>
Creating a Variable
You'll probably want to do more with your forms than display their contents in a
Web page. For example, based on the contents of the form, you may want to
create a variable and insert that variable in different places of your response
page. You may need to create a variable. To do that, just make up a name and set
it equal to the contents of the field.
For example, if you have a field called "CatName" in your form, you can save it
into a variable called "TheName" by typing:
<% TheName = Request.Form("CatName") %>
If you want to display "VisitorName" several times within a text you only need to
include the variable in the text. For example:
My cat´s name is <% =TheName %>. Do you want to see <% =TheName %>?.
Example
The form in this example asks users to introduce their names and their favorite
color: red, blue, or green. When the form is received, the server responds
displaying these data.
nameandcolor.html
<html>
<head><title>Name and Color</title></head>
<body>
<FORM ACTION="nameandcolor.asp" METHOD=POST>
Let me know your Name and Favorite Color:
<P>YOUR NAME:
<INPUT TYPE="TEXT" NAME="YOURNAME" SIZE=20>
<P>COLOR:
<INPUT TYPE="RADIO" NAME="COLOR" VALUE="1" CHECKED>Red
<INPUT TYPE="RADIO" NAME="COLOR" VALUE="2">Green
<INPUT TYPE="RADIO" NAME="COLOR" VALUE="3">Blue
<P>
<INPUT TYPE="SUBMIT" VALUE="OK">
</FORM>
</body>
</html>
Now, create an ASP file ("nameandcolor.asp") and cut-and-paste the following text
into it.
nameandcolor.asp
<html>
<head><title>Name and Color</title></head>
<body>
<% TheName = Request.Form("YOURNAME") %>
<% colornumber = Request.Form("COLOR") %>
Hi, <% =Thename %>.<BR>
I know your favorite color is
<% if colornumber = "1" then %>
red
<% end if %>
<% if colornumber = "2" then %>
green
<% end if %>
<% if colornumber = "3" then %>
blue
<% end if %>.
</body>
</html>
array.asp
<html> 1 Line 6: In this example we have defined by
<title>My Array</title> 2 using "DIM" an array named Mydata and we
<body> 3 have defined the size of the array. We may
4 consider the table bellow as the source of
<% 5 information for our array.
DIM MyData(2,2) 6 MyData 0 1 2
MyData (0,0) = "1" 7
MyData (0,1) = "2" 8 0 1 2 3
MyData (0,2) = "3" 9
MyData (1,0) = "4" 1 4 5 6
10
MyData (1,1) = "5" 11 2 7 8 9
MyData (1,2) = "6" 12
MyData (2,0) = "7" 13 Lines 7-15. After defining the array we have
MyData (2,1) = "8" 14 assigned values to the array.
MyData (2,2) = "9" 15
16
Response.write (MyData (1,2)) 17 Line 17. In the response page we will send the
%> value assigned to MyData(1,2) in the array. The
18 first number will be the row and the second the
19 column, so that in our case the response page
</body> 20 will show the value "6"
</html> 21
Very often, we will defined an array from data obtained from a table with only one
column. Let´s check an example:
array2.asp
<html> 1 Original table for the array in the script
<title>My Array</title> 2 MyData
<body> 3
4 0 1
<% 5
DIM MyData(9) 6 1 4
MyData (0) = "0" 7
MyData (1) = "1" 8 2 7
MyData (2) = "2" 9
MyData (3) = "3" 10 3 3
MyData (4) = "4" 11
MyData (5) = "5" 12 4 4
MyData (6) = "6" 13 5 5
MyData (7) = "7" 14
MyData (8) = "8" 15 6 6
MyData (9) = "9" 16
17 7 7
Response.write (MyData (5)) 18 8 8
%> 19
20 9 9
</body> 21
</html> In the response page we will send the value
assigned to MyData(5) in the array. The response
page will return 5.
In the examples above we have defined all the values within the script one by one,
but this assignation may be done in a different way, as it is described in the
example bellow:
</pre>
In this example the array has been create from a string, and each component of the
array has been separated by a comma. The Array method will do it for us easily.
We may also want to use a different string with a different delimiter to define the
components in our array:
</pre>
In this example we have defined the variable TheText, and whithin this variable
we have include strings separated by "=".
In the next line, we have split the variable TheText into an array of strings
(Thearray).
Split command have been used to brake TheText and "=" has been used as a
delimiter to separate the substrings.
In the response page we have indicated the individual values of Thearray one by
one.
It may happend to have a variable we want to split, but we do not know how many
substrings we may get. In that case we may use ubound command to discover
how many elements are in our array, and them we may use that value to write them
by using a For-next loop (see example below).
</pre>
In the next example we will filter the information in our array, and we will display
only part of it.
array5.asp
<pre> Find strings containing "t" (case sensitive)
<% Eight
dim MyArray(9) Find strings containing "t"
MyArray (0) = "Zero" Two,Three,Eight
MyArray (1) = "One" Find strings which do not contain "t" (case sensitive)
MyArray (2) = "Two" Zero,One,Two,Three,Four,Five,Six,Seven,Nine
MyArray (3) = "Three" Find strings which do not contain "t"
Zero,One,Four,Five,Six,Seven,Nine
MyArray (4) = "Four"
MyArray (5) = "Five"
MyArray (6) = "Six"
MyArray (7) = "Seven"
MyArray (8) = "Eight"
MyArray (9) = "Nine"
%>
</pre>
The array and the assignation of values has been done as usually, and in the second
part of the script we have used some lines similar to this one:
<% =join(filter(MyArray,"t",True,0),",") %>
In this lines we have filter the values at MyArray and we have join them.
filter(MyArray,"t",True,0)
This part of the line have search for "t" in MyArray.
True means we have selected the strings containing the search string (in this case
"t").
False will indicate we are selecting the strings which do not content the search
string.
0 means our search is case sensitive (a binary comparation)
1 will mean it is not a case sensitive search (a textual comparation)
join(filter(MyArray,"t",True,0),",")
The complete line will join the filtered strings with the delimiter indicated (in this
case ",")
Creating a table from data in a string
In order to undertand this script we will consider we have a table like the one
bellow, and that this table was the original source of information we used to create
our table:
Pete Chica 12 From the table we got this three lines by separeting the values by
student commas:
r go 3
Joh Londo 23
teacher Peter,student,Chicago,123
n n 4
John,teacher,London,234
Manag 78 Sue,Manager,Sidney,789
Sue Sidney
er 9
And finaly we conected the three lines by separeting the values
with "/":
Peter,student,Chicago,123/John,teacher,London,234/Sue,Manage
r,Sidney,789
The string obtained was saved to a variable named Mydata in the script bellow.
The resulting page will show a table like the original. This script is not limited by
number of rows or columns (the maximun amount of then is calculate each time
we run the script).
Createatable.asp
<%
Mydata="Peter,student,Chicago,123/John,teacher,London,234/Sue,Manager,Sidney,789"
Createtable()
%>
<%
Sub CreateTable()
MyRows=split (Mydata,"/")
RowsNumber=ubound(MyRows)
End Sub
%>
This script may be used for several porpouses: we may generate Mydata by
filtering values from an array as shown bellow:
<%
Dim Myclients(3)
Myclients(0)="Peter Smith,Chicago,Manager,123"
Myclients(1)="John Smith,New York,Accountant,124"
Myclients(2)="George Smith,Chicago,Administration,245"
Myclients(3)="Sam Smith,Dallas,Consultant,567"
SearchFor="Chicago"
Mydata=join(filter(Myclients,SearchFor,True,1),"/")
Createtable()
%>
In this example, in our first visit a form asking for a keyword will be display. After
submitting the keyword Toredirect() Subroutine will be activated.
In this Subroutine we have create two arrays: Myinfo has a description of the URL
located at MyURL. In case the keyword is included in the description of the site,
the visitor will be redirected to the corresponding URL. Both arrays may be very
very long.
search.asp
<% if request.form="" them %>
<form method=post action=search.asp>
<input type=text name=keyword>
<input type=Submit value=Search>
</form>
<%
else
Toredirect()
end if
%>
<%
Sub Toredirect()
dim Myinfo(4)
Myinfo (0) = "Asp tutorial for beginners"
Myinfo (1) = "Displaying Date Time and Text"
Myinfo (2) = "Using Variables and Forms"
Myinfo (3) = "If...Then and For...Next instructions"
Myinfo (4) = "Do...Loop and Select...Case instructions"
dim MyURL(4)
MyURL (0) = "http://www.asptutorial.info"
MyURL (1) = "http://www.asptutorial.info/Datetime.htm"
MyURL (2) = "http://www.asptutorial.info/Forms.htm"
MyURL (3) = "http://www.asptutorial.info/if_then-for_next.htm"
MyURL (4) = "http://www.asptutorial.info/Do_loop-Select_case.htm"
Numberofpairs=ubound(Myinfo)
For n=0 to Numberofpairs
if inStr(Myinfo (n), request.form ("keyword"))>0 then
Response.redirect(MyURL(n))
end if
Next
In order to learn how Dictionary object works we will create a small script which will
translate number 1 to 10 from English to Spanish.
translate.asp
1
<% 2
SET MyDictionary=CreateObject("Scripting.Dictionary") 3
MyDictionary.Add "one","uno" 4
MyDictionary.Add "two","dos" 5
MyDictionary.Add "three","tres" 6
MyDictionary.Add "four","cuatro" 7
MyDictionary.Add "five","cinco" 8
MyDictionary.Add "six","seis" 9
MyDictionary.Add "seven","siete" 10
MyDictionary.Add "eight","ocho" 11
MyDictionary.Add "nine","nueve" 12
MyDictionary.Add "ten","diez" 13
EnglishNumber="four" 14
SpanishNumber=MyDictionary.Item (EnglishNumber) 15
Response.Write(SpanishNumber) 16
%> 17
18
When adding pairs of English and Spanish numbers to the Dictionary object, the
number writen in English is a Key, and the number writen in Spanish a Item.
We may change the values in our dictionary by using this kind of code:
In our original script the key "one" will be substitute by a new key value ("1"). The
item "uno" will not be changed.
In our original script the item corresponding to key "two" will be substitute by a new
item value ("2"). The key "two" will not be changed.
We may display the number of element pairs in the dictyonary by using this code:
MyDictionary.Count example
If we want to check whether a key exists in our dictionary before responding to our visitor we
will use this kind of comparisoncode
If there is no information posted to the script (line 6), the script will send to the visitor
the form in the Sendform() subrouting (lines 34-40).
When a request to translate a number is get the script will check whether the number
corresponds to a key in the dictionary (line 25). If the response is afirmative the
corresponding item is send to the visitor. In case the key does not exists, a "No
translation available" response is send to the visitor (line 29).
translation.asp
<html> 1
<title>Page under construction</title> 2
<body> 3
4
<% 5
if request.form="" then 6
Sendform() 7
else 8
SET MyDictionary=CreateObject("Scripting.Dictionary") 9
10
MyDictionary.Add "one","uno" 11
MyDictionary.Add "two","dos" 12
MyDictionary.Add "three","tres" 13
MyDictionary.Add "four","cuatro" 14
MyDictionary.Add "five","cinco" 15
MyDictionary.Add "six","seis" 16
MyDictionary.Add "seven","siete" 17
MyDictionary.Add "eight","ocho" 18
MyDictionary.Add "nine","nueve" 19
MyDictionary.Add "ten","diez" 20
EnglishNumber=request.form("EnglishNumber") 21
Response.Write("English number: " & EnglishNumber) 22
23
if MyDictionary.Exists (EnglishNumber)=True then 24
SpanishNumber=MyDictionary.Item (EnglishNumber) 25
Response.Write("<BR>Spanish number: " & SpanishNumber) 26
else 27
Response.Write("<BR>Spanish number: " & "No translation available") 28
end if 29
end if 30
%> 31
32
<% Sub Sendform() %> 33
<form action=translation.asp method=post> 34
Write a number in English<BR> 35
<input type=text size=30 name=EnglishNumber><BR> 36
<input type=submit Value="Enter to my Secret Page"> 37
</form> 38
<% End Sub %> 39
40
</body> 41
</html> 42
43
In this example keys and items are used as usernames and passwords. It is very similar to the
one above.
secretpage.asp
<% 1
if request.form="" then 2
Sendform() 3
else 4
SET MyDictionary=CreateObject("Scripting.Dictionary") 5
MyDictionary.Add "John","123" 6
MyDictionary.Add "Peter","456" 7
MyDictionary.Add "Anna","789" 8
Username=request.form("Username") 9
Password=request.form("password") 10
11
if MyDictionary.Exists (Username)=True AND 12
Password=MyDictionary.Item (Username) then 13
SecretInfo() 14
else 15
Response.Write("Error: incorrect userame or password") 16
end if 17
end if 18
%> 19
20
<% Sub Sendform() %> 21
<form action=secretpage.asp method=post> 22
Username: <input type=text size=30 name=Username><BR> 23
Password: <input type=password size=30 name=Password><BR> 24
<input type=submit Value="Submit"> 25
</form> 26
<% End Sub %> 27
28
29
<% Sub SecretInfo() %> 30
<html> 31
<head> 32
<title>My Secret Page</title> 33
</head> 34
<body bgcolor=FFFFFF> 35
<center> 36
<h1>This is my secret info</h1> 37
Hello !<BR> 38
Do you want to be my friend? 39
</center> 40
</body> 41
</html> 42
<% End Sub %>