Title: Regular Expression continue and Cookies
1Regular Expression (continue)and Cookies
2Quick Review
- What letter values would be included for the
following variable, which will be used for
validation purposes - var validCharacters /A-Z/
- a. Only A and Z would be included.
- b. Both uppercase and lowercase letters would be
included. - c. Only lowercase letters A-Z would be included.
- d. Only uppercase letters A-Z would be included.
3Quick Review
- What letter values would be included for the
following variable, which will be used for
validation purposes - var validCharacters /A-Z/
- a. Only A and Z would be included.
- b. Both uppercase and lowercase letters would be
included. - c. Only lowercase letters A-Z would be included.
- d. Only uppercase letters A-Z would be included.
4Quick Review
- What object is needed to create a regular
expression? - a. RE
- b. RegExp
- c. RegularExp
- d. RegExpression
-
5Quick Review
- What object is needed to create a regular
expression? - a. RE
- b. RegExp
- c. RegularExp
- d. RegExpression
-
6Meta characters in Regular Expression(continue)
- \ / ( ) ? .
- / means the ends of regular expression
- . means any character(s)
- ton. Matches tons, tonneaus but not ton
All characters in this list need to be escaped if
we want to use them as the characters themselves
without their special meanings.
7Alternation
- choice
- Example /JohnKarenSteve/
- Group subpattern
- Example /(SamDanTom)Kat/
8Form Validation with Regular Expressions
- Check for alphabetic data (Examples first name
or last name) - /a-zA-Z/
-
9Form Validation with Regular Expressions
- Check for valid social security number
- Pattern lt3 digitsgt-lt2 digitsgt-lt4 digitsgt
- - (dash) optional
- What is the regular expression?
-
/d3-?\d2)-?\d4/
10Form Validation with Regular expression
- Valid phone number
- Patternlt3 digitsgtltdash and/or spacesgtlt3
digitsgtltdash and/or spacesgtlt4 digitsgt - Spaces optional
- What is the regular expression?
/\d3-?\s\d3)-?\s\d4/
11Form validation
- Checking for valid (format) email addresses
- _at_ username_at_address.domainname
- at least six characters
- domain name 2 characters
-
/((\w)\.?)_at_((\w)\.?)\.a-zA-Z2,4/
12Examples of regular expressions
- To test a string begins with letters between a
and f (lowercase only) - /a-f/
- To test if a string contains a number
- /0-9/
- or /\d/
-
13Examples of regular expressions
- To test if a string ends with three numbers
- /0-93/
- To test if 1-character string doesnt contain
number 4 or letter a - /a4/
14Practice
- Step 1 Change your practice in Week 10 to
validate an international phone number in a
function validatePhone - 011ltspace(s)gtlt2 digitsgtltspace(s)gtlt3
digitsgtltspace(s)gtlt5 digitsgt - Step 2 Insert a textfield that represents a UK
zip code. If user finishes typing and press tab
to move to the next component, call the function
validateUKZipCode - Step 3 Develop the function validateUKZipCode
for checking UK zip code. Start with one or two
characters - Followed by a one or two digits
- Followed by a space
- and Followed by a digit and two characters
- Example RH1 2QP
- R12 2QP
-
15Objectives
- Create cookies to store bits of information
- Create cookies with multiple parameters
- Read cookies and use their values
- Delete cookies when you are finished with them
16Client Side Cookies
- Cookies are text files used to store information
about the user of your web site. Cookies commonly
store user names and encrypted passwords for
protected sites. They can also include items
purchased at online stores. Many web sites store
information to achieve a personalized page for
the user. - Generally any information can be stored in a
cookie. - Browsers since Netscape Navigator 2 and Internet
Explorer 3 have supported the use of cookies.
17Client Side Cookies
- Cookies are written to and read from a text file
stored on the users computer. This is
accomplished using the cookie property of the
document object. - Cookies stored in Mozilla based browsers (like
Netscape) are stored in a file called
cookies.txt. All cookies are stored in a common
single text file. - Cookies stored in Internet Explorer are saved in
a domain-specific text file. Each cookie has a
separate text file.
18Client Side Cookies Data
- Cookies contain fields of information as shown
below - The domain of the server that created the cookie
- The name of the cookie
- The string of data being stored in the cookie
- The expiration date for the cookie (optional)
- A boolean value indicating whether you need a
secure HTTP connect to access the cookie
(optional) - A path indicating the URL path(s) that can access
the cookie (optional)
19Example of a real cookie
- CP
- null
- www.missworld.tv/
- 1088
- 1761935360
- 30785590
- 256242976
- 29811669
20Client Side Cookies
- Cookies can be created with or without an
expiration date. When they are created without an
expiration date they are considered to be
temporary. Otherwise they are stored (persistent)
cookies. - A temporary cookie is valid and exists only for
the current session of the browser. When the user
exits the browser, the cookie is automatically
deleted.
21Cookies attributes
- Name
- nameofcookie value
- Example name Bob
- Expiration data
- expiresWeekday, DD-MON-YY HHMMSS GMT
- Example
- expires Friday, 15-Mar-06 120000 GMT
- Domain name
- domain.domain_name
- Example
- domain.kajinsky.com
22Cookies attributes
- Path
- path pathname
- Example path /home
- Secure
- secure
23Escape and unscape() build in functions
- We can not use whitespace, semicolons, and
commas. - escape() function encode the string object by
converting all non-alphanumeric characters to
their hexadecimal equivalent, preceded by - Unescape() converts the encoded string back to
its original format
24Creating a Cookie with Javascript
- Cookie is stored by Javascript as a document
object for both reading and writing cookie data - document.cookie contains a string of namevalue
pairs representing the names of all the cookiers
and their corresponding values.
25Lets make a cookie
26Retrieving cookies from a server
Delete a cookie
27Client Side Cookies Security
- Most browsers do not store the information in
cookies to the text file immediately. Often the
information is stored in the memory of the
computer. When you exit the browser, the cookies
are written to file. - For e-commerce sites two additional security
measures are taken - Boolean value indicating whether you need a
secure HTTP connection - A path indicating the URL path(s) that can access
the cookie
28Client Side Cookies Storage Limit
- In most browsers you have a maximum of 20 cookies
per domain. Netscape browsers have a total limit
of 300 stored cookies. - You should limit each cookie to a maximum length
of 2,048 characters.
29Encoding Information Stored in Cookies
- Cookie data cannot be stored with spaces,
semicolons, or commas you should URL encode the
data. - Cookies should be stored as namevalue pairs. For
example - userNameWilliam Stanek
- When storing the information, the escape()
function is used. The escape function takes
replaces non printable text characters as escape
codes. For example, a space is stored as 20.
When reading in the data in a cookie the
unescape() function is used to reverse the
encoding.
30Client Side Cookies Expiration Date and Update
- The expiration date used in a cookie is always
formatted to Greenwich Mean time using the GMT
method. - var now new Date()
- expDate now.toGMTString()
- To update a cookie that has already been set,
simply save the cookie as you did the first time
it was created. - document.cookie cookieInfo (where cookieInfo is
the string of information containing the data and
other cookie parameters)
31Extracting Data From Cookies
- Data retrieved from a cookie is a simple text
string. While there is no specific JavaScript
function for parsing the information from the
text string, you can build a function using the
indexOf() method. - The text string returned from the cookie contains
only the namevalue pairs of information. - var myCookie document.cookie
32Summary
- Cookies are widely used on the WEB
- They make tracking information about the user
practical - There are two types of cookies
- Temporary no expiration date
- Stored future expiration date
- Deleting a cookie in JavaScript is accomplished
by setting its expiration date to an earlier date
than now - Cookies can be updated
- Cookie information must be encoded using the
escape() method and decoded using the unescape()
method.
33Practice
- Step 1 Use the following code as your framework
for this practice - lthtmlgtltheadgtlttitlegtMaking a Cookielt/titlegt
- ltscript language"JavaScript"gt
- // insert your Javascvript code here
- lt/scriptgt
- lt/headgt
- ltbody gt
- lt!-- Insert your form here --gt
- lt/bodygt
- lt/htmlgt
34Practice
- Step 2 Use cookie2.htm as an example create a
form that asks for a users favorite color - Step 3 Also use cookie2.htm as an example,
create a function makeCookie in which you store
the users favorite color in a cookie - Step 4 Test it in IE.
-
35Practice
- Step 4 Use cookie3.htm as an example, write a
function to read the users favorite color from a
cookie and set the document background to that
color. Name this function as setBackgroundColor - Step 5 Locate the body tag and change it to
- ltbody onLoad "javascriptsetBackground()"gt
- Step 6 Test it in IE
-