Title: CS 330 Class 7
1CS 330 Class 7
- Comments on Exam
- What is happening in GetDay? GetMonth? GetYear?
- What can you learn from p. 445-466?
- Programming plan for today
- Review approaches to form validation
- Regular expressions
- Form validation with regular expressions
- Beginnings of CGI
2Form Validation
- How can we identify a valid zip?
- length 5
- all digits.
- Valid phone number (xxx-xxxx)
- A bit harder Check each position for 3 digits,
one dash, 4 digits - Valid email
- must contain _at_
- must contain .
- _at_ comes before .
- Extension should be com, org, gov, net, mil
- See guestans.htm
- Did you use any ideas from the text?
3Regular Expressions
- An object that describes a pattern of characters.
- Specified by including it within / and /
- var pattern / \d2,4/ \d digit,
2,4 2 - 4 - Character classes
- . anything except newline
- \d any digit
- \s any whitespace
- \w any letter or digit
- abc a, b, or c
- abc anything but a, b, or c
- A-Za-z any letter
- 0-9 same as \d
- red blue red or blue
- Example a letter followed by a digit /A-Za-z
\d /
4Regular Expressions
- Repetition
- n, m at least n but no more than m
- ? 0 or 1 occurrences
- one or more
- zero or more
- Example 7 digit telephone / \d3 - \d 4 /
- Specifying match position
- the beginning of the string
- the end
- start with capital, end with period /A-Z
\. - Exercises to do now
- a pattern that contains a word with an optional
period - a pattern that contains Fred with a space (not
Freddy) - a url of the form aurora.wells.edu
5Regular Expressions
- Methods for testing if a pattern is present. e.g.
- thePattern /d5/
- theZip 12212
- Using match (a String method with RegExpr
parameter) - theZip.match(thePattern) is true if theZip
contains pattern - if (theZip.match(aPattern))
-
- Using test (a RegExpr method with String
parameter) - aPattern.test(theZip) is true if aString
contains pattern - if (pattern.test(theZip))
- Is this enough to check a zipcode? How about
zip4? - Wont cover other methods search, replace, and
split for RegExpr, and exec for String..
6Introduction to CGI
- Common gateway interface
- References CS 330 page
- CGI allows interactions among client browser, web
server, and traditional applications. E.g. - Processing forms
- Gateways (to services not immediately available
to the client, e.g. a database) - Virtual documents (tailored to the users input,
e.g. results from Yahoo) - CGI programs are run on the server
- Results are often sent back in a client page
7 Client/Server Communication
Client (1)
Server (3)
(2)
client program
server program
(4)
(5)
Steps (1) Client program makes
connection to server program (2) Client sends
HTTP request message (3) Server processes
HTTP request message (4) Server sends HTTP
response message (5) Server closes the
connection
8 CGI Communication
Server Machine
(1)
Client
Server SW
(5)
(4)
(2)
Other servers
CGI Script (3)
Other programs
(1) Server SW decodes client HTTP request (2)
Server SW sets variables and executes CGI
script (3) CGI script runs (4) Script returns
output with CGI headers to server SW (5) Server
translates output and headers into HTTP response
to client
9Languages for CGI Scripts
- UNIX shell commands
- C, C
- Perl
- (practical extraction and report language)
- Visual Basic
- Why Perl?
- Portable
- Powerful string manipulation operations
- C-like syntax
10CGI With No Parameters
- hello.htm invokes script hello.cgi via
- ltagt ref"http/../scripts/hello.cgi"gt
Hello Worldlt/agt - This generates HTTP header GET
../scripts/hello.cgi - Server executes script and sends output to client
- the print statements create an HTML document
which is interpreted by the browser - Conventions
- scripts are usually stored in a separate
directory and must have execute permission - the first line indicates the language
- the second is part of the returned HTTP header
11Next
- How do we write programs that accept and use
information from the client?