Regular Expression continue and Cookies - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Regular Expression continue and Cookies

Description:

The text string returned from the cookie contains only the name=value pairs of information. ... Step 4: Test it in IE. Practice ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 36
Provided by: Sta4
Category:

less

Transcript and Presenter's Notes

Title: Regular Expression continue and Cookies


1
Regular Expression (continue)and Cookies
2
Quick 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.

3
Quick 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.

4
Quick Review
  • What object is needed to create a regular
    expression?
  • a. RE
  • b. RegExp
  • c. RegularExp
  • d. RegExpression

5
Quick Review
  • What object is needed to create a regular
    expression?
  • a. RE
  • b. RegExp
  • c. RegularExp
  • d. RegExpression

6
Meta 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.
7
Alternation
  • choice
  • Example /JohnKarenSteve/
  • Group subpattern
  • Example /(SamDanTom)Kat/

8
Form Validation with Regular Expressions
  • Check for alphabetic data (Examples first name
    or last name)
  • /a-zA-Z/

9
Form 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/
10
Form 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/
11
Form 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/
12
Examples 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/

13
Examples 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/

14
Practice
  • 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

15
Objectives
  • 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

16
Client 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.

17
Client 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.

18
Client 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)

19
Example of a real cookie
  • CP
  • null
  • www.missworld.tv/
  • 1088
  • 1761935360
  • 30785590
  • 256242976
  • 29811669

20
Client 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.

21
Cookies 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

22
Cookies attributes
  • Path
  • path pathname
  • Example path /home
  • Secure
  • secure

23
Escape 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

24
Creating 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.

25
Lets make a cookie
26
Retrieving cookies from a server
Delete a cookie
27
Client 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

28
Client 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.

29
Encoding 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.

30
Client 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)

31
Extracting 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

32
Summary
  • 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.

33
Practice
  • 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

34
Practice
  • 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.

35
Practice
  • 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
Write a Comment
User Comments (0)
About PowerShow.com