Presentation is loading. Please wait.

Presentation is loading. Please wait.

Regular Expression (continue) and Cookies. Quick Review What letter values would be included for the following variable, which will be used for validation.

Similar presentations


Presentation on theme: "Regular Expression (continue) and Cookies. Quick Review What letter values would be included for the following variable, which will be used for validation."— Presentation transcript:

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: /John|Karen|Steve/ Group: subpattern Example: /^(Sam|Dan|Tom)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: - - - (dash): optional What is the regular expression? /^d{3}-?\d{2)-?\d{4}$/

10 Form Validation with Regular expression Valid phone number: Pattern: Spaces: optional What is the regular expression? /^\d{3}-?\s*\d{3)-?\s*\d{4}$/

11 Form validation Checking for valid (format) email addresses @: username@address.domainnameusername@address.domain at least six characters domain name: 2 characters /^((\w+)\.?)+@((\w+)\.?)+\.[a-zA-Z]{2,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-9]{3}$/ To test if 1-character string doesn’t 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 011 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 user’s 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: 1.The domain of the server that created the cookie 2.The name of the cookie 3.The string of data being stored in the cookie 4.The expiration date for the cookie (optional) 5.A boolean value indicating whether you need a secure HTTP connect to access the cookie (optional) 6.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 Cookie’s attributes Name: nameofcookie = value Example: name = Bob Expiration data ;expires=Weekday, DD-MON-YY HH:MM:SS GMT Example: ;expires= Friday, 15-Mar-06 12:00:00 GMT Domain name ;domain=.domain_name Example ;domain=.kajinsky.com

22 Cookie’s 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 name=value pairs representing the names of all the cookiers and their corresponding values.

25 Let’s 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: 1.Boolean value indicating whether you need a secure HTTP connection 2.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 name=value pairs. For example: userName=“William 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 name=value 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: Making a Cookie // insert your Javascvript code here

34 Practice Step 2: Use cookie2.htm as an example: create a form that asks for a user’s favorite color: Step 3: Also use cookie2.htm as an example, create a function makeCookie in which you store the user’s 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 user’s 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: Step 6: Test it in IE


Download ppt "Regular Expression (continue) and Cookies. Quick Review What letter values would be included for the following variable, which will be used for validation."

Similar presentations


Ads by Google