web development -2

profilesri999
t7.pptx

HTML5, CSS3, and JavaScript

6th Edition

Designing a Web Form

Tutorial 7

Carey

XP

XP

XP

XP

XP

Objectives

Explore web forms

Work with form servers

Create forms and field sets

Create labels and input boxes

Explore form layout

Work with date and time fields

Create a selection list

2

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

2

Objectives (continued)

Create option buttons

Create check boxes and text area boxes

Create spinners and range sliders

Use data lists

Create form buttons

Validate a form

Apply validation styles

3

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

Structure of a Web Form

4

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

4

Introducing Web Forms

Web form

Allows users to enter data that can be saved and processed

Common way to accept user input

Allows the creation of interactive websites for user feedback

5

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

5

Parts of a Web Form

Controls, also known as widgets, are the objects that allow a user to interact with a form

Each data entry control is associated with a data field

Data field: Stores the data values supplied by a user

6

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

6

Parts of a Web Form (continued 1)

Types of controls

Input boxes to insert text and numeric values

Option/radio buttons to select data values from a predefined set of options

Selection lists to select data values from an extensive list of options

Check boxes to select data values limited to two possibilities, such as “yes” or “no”

Text area boxes to enter text strings that may include several lines of content

7

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

7

Parts of a Web Form (continued 2)

Types of widgets

Spin boxes to enter integer values confined to a specified range

Slider controls to enter numeric values confined to a specified range

Calendar controls to select date and time values

Color pickers to choose color values

8

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

8

Parts of a Web Form (continued 3)

9

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

9

Forms and Server-Based Programs

Field values entered by a user are processed by a program running on the user’s computer or on a web server in a secure location

Example: A web form is used to collect data from a customer for an order and the server program processes the data and handles the billing and delivery

10

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

10

Forms and Server-Based Programs (continued)

11

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

11

Starting a Web Form

Web forms are marked using the form element

<form id=“text” attributes>

content

</form>

id identifies the form

attributes specify how the form should be processed by the browser

content is the form’s content

12

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

12

Starting a Web Form (continued)

A form element can be placed anywhere within the body of a page

Forms also can contain page elements such as tables, paragraphs, inline images, and headings

13

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

13

Interacting with the Web Server

The action, method, and enctype attributes have to be included in a form to specify where and how to send the form data

<form action=“url” method=“type” enctype=“type”>

content

</form>

14

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

14

Interacting with the Web Server (continued 1)

action attribute provides the location of the web server program that processes the form

method attribute specifies how the browser should send form data to the server

enctype attribute specifies how the form data should be encoded as it is sent to the server

15

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

15

Interacting with the Web Server (continued 2)

Two possible values for method attribute

Get method: Tells the browser to append the form data to the end of the URL specified in the action attribute

The get method is the default method

Post method: Sends the form data in its own separate data stream

The post method is considered to be a more secure form of data transfer

16

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

16

Interacting with the Web Server (continued 3)

The enctype attribute has three possible values

17

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

17

Interacting with the Web Server (continued 4)

18

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

18

Interacting with the Web Server (continued 5)

A script element is an HTML element used to access and run JavaScript programs that will run within the user’s browser

19

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

19

Creating a Field Set

Field set: Groups fields that share a common purpose

Field sets are created using the fieldset element

<fieldset id=“id”>

content

</fieldset>

id identifies the field set

content is the form content within the field set

20

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

20

Marking a Field Set

21

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

21

Adding a Field Set Legend

Legend describes the content of a field set using the legend element

<legend>text</legend>

where text is the text of the legend

The legend element contains only text and no nested elements

By default, legends are placed in the top-left corner of the field set box and can be moved to a different location using the CSS positioning styles

22

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

22

Adding a Field Set Legend (continued)

23

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

23

Creating Input Boxes

Syntax for the input element

<input name=“name” id=“id” type=“type” />

The name attribute provides the name of the data field associated with the control

The id attribute identifies the control in which the user enters the field value

The type attribute indicates the data type of the field

24

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

24

Creating Input Boxes (continued 1)

25

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

25

Creating Input Boxes (continued 2)

26

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

26

Input Types and Virtual Keyboards

Virtual keyboards are software representations of a physical device

Web forms can be made responsive by displaying different virtual keyboards for each input type

Example: An input box for telephone number is more convenient to read with digits displayed prominently on the keyboard

27

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

27

Adding Field Labels

To associate a text string with a control, the text string has to be enclosed within the label element

<label for=“id”>label text</label>

id is the id of the control that is associated with the label

label text is the text of the label

28

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

28

Adding Field Labels (continued)

29

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

29

Designing a Form Layout

There are two general layouts

Labels are placed directly above the input controls

Labels and controls are placed side-by-side

30

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

30

Defining Default Values and Placeholders

The value attribute is used to specify a default field value

31

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

31

Defining Default Values and Placeholders (continued 1)

Placeholders: Text strings that appear within a form control, providing a hint about the kind of data that should be entered into a field

They are defined using the placeholder attribute

placeholder=“text”

where text is the text of the placeholder

32

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

32

Defining Default Values and Placeholders (continued 2)

33

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

33

Defining Default Values and Placeholders (continued 3)

34

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

34

Entering Date and Time Values

Date and time fields ensure that users enter data in the correct format

Indicated using type attributes: date, time, datetime-local, month, and week

35

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

35

Creating a Selection List

A selection list is a list box that presents users with a group of possible values for the data field

The list is created using the select and option elements

<select name=”name”>

<option value=”value1”>text1</option>

<option value=”value2”>text2</option>

...

</select>

36

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

36

Creating a Selection List (continued 1)

name is the name of the data field

value1, value2,… are the possible field values

text1, text2,… are the text of the entries in the selection list that users see on the web form

37

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

37

Creating a Selection List (continued 2)

38

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

38

Working with Select Attributes

By default, a selection list appears as a drop-down list box

To display a selection list as a scroll box, use the size attribute to the select element

<select size=“value”> ... </select>

where value is the number of options that the selection list displays at one time

39

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

39

Working with Select Attributes (continued 1)

By default, selection lists allow only one selection from the list of options

To allow more than one item to be selected, add multiple attribute

<select multiple> ... </select>

40

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

40

Working with Select Attributes (continued 2)

Two ways for users to select multiple items from a selection list

For non-contiguous selection, press and hold the Ctrl key while making the selections

For contiguous selection, select the first item, press and hold the Shift key, and then select the last item in the range

41

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

41

Grouping Selection Options

Organize selection list options by placing them in option groups using the optgroup element

<select>

<optgroup label=”label1”>

<option>text1</option>

<option>text2</option>

</optgroup>

</select>

where label1 is the label for the different groups of options

42

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

42

Grouping Selection Options (continued)

43

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

43

Creating Option Buttons

Option buttons are also called radio buttons

Unlike selection lists, the options appear as separate controls in the web form

They are created with a group of input elements with a type attribute value of “radio”

<input name=“name” value=“value1” type=“radio” />

44

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

44

Creating Option Buttons (continued 1)

<input name=“name” value=“value2” type=“radio” />

where name is the name of the data field and value1, value2, value3,… are the field values associated with each option

Set an option button to be selected as the default by adding the checked attribute to the input element

<input name=”name” type=”radio” checked />

45

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

45

Creating Option Buttons (continued 2)

46

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

46

Creating Check Boxes

Check boxes are designed for fields that record the presence or absence of an object or event

They are created using the input element with the type attribute set to “checkbox”

<input name=“name” value=“value” type=“checkbox” />

value attribute contains the value of the field when the check box is checked

type attribute indicates that the input box is a check box

47

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

47

Creating Check Boxes (continued)

By default, a check box is not checked

48

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

48

Creating a Text Area Box

Text area is created using the textarea element

<textarea name=“name”>

text

</textarea>

where text is the default value of the data field

49

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

49

Creating a Text Area Box (continued 1)

HTML supports the rows and cols attributes to set the text area size

<textarea rows=”value” cols=”value”>

...

</textarea>

rows attribute specifies the number of lines in the text area box

cols attribute specifies the number of characters per line

50

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

50

Creating a Text Area Box (continued 2)

51

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

51

Entering Numeric Data

Creating a Spinner Control

Spinner control: Displays an up or down arrow to increase or decrease the field value by a set amount

To create a spinner control, apply the input element using the number data type

52

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

52

Entering Numeric Data (continued 1)

53

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

53

Entering Numeric Data (continued 2)

Creating a Range Slider

Slider control: Limits a numeric field to a range of possible values

To create a slider control, apply the range data type in the input element

54

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

54

Entering Numeric Data (continued 3)

55

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

55

Suggesting Options with Data Lists

Data list: A list of possible data values that a form field can have

Data lists are defined using the datalist element

<datalist id=”id”>

<option value=”value” />

<option value=”value” />

</datalist>

56

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

56

Suggesting Options with Data Lists (continued)

57

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

57

Working with Form Buttons

Form buttons: A type of form control that performs an action

Actions performed

Run a command from a program linked to the web form

Submit the form to a program running on the web server

Reset the form fields to their default values

58

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

58

Creating a Command Button

Command button: Runs a program that affects the content of a page or the actions of a browser

Created using the input element with the type attribute set to button

<input value=“text” onclick=“script” type=“button” />

text is the text that appears on the button

script is the name of the program code that is run when the button is clicked by the user

59

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

59

Creating Submit and Reset Buttons

Submit button: Submits a form to the server for processing when clicked

Submit button is created using input elements with the type attribute set to “submit” and “reset” respectively

<input value=“text” type=“submit” />

where text is the text string that appears on the button

60

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

60

Creating Submit and Reset Buttons (continued 1)

Reset button: Resets a form, changing all fields to their default values and deleting any field values that a user has entered

Reset button is created using input elements with the type attribute set to “reset”

<input value=“text” type=“reset” />

where text is the text string that appears on the button

61

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

61

Creating Submit and Reset Buttons (continued 2)

62

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

62

Designing a Custom Button

The appearance of a command, submit, and reset button is determined by the browser

For more control over a button’s appearance use the button element

<button type=“text”>

content

</button>

type attribute specifies the button type

content are HTML elements placed within the button

63

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

63

Validating a Web Form

Validation: Process of ensuring that a user has supplied valid data

Types of validation

Server-side validation – validation occurs on the web server

Client-side validation – validation occurs in the user’s browser

64

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

64

Identifying Required Values

The first validation test is to verify if data is supplied for all the required data fields

Add the required attribute to the control to identify the required data fields

If a required field is left blank, the browser will not submit the form returning an error message to indicate the unavailability of data

65

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

65

Validating Based on Data Type

A form fails the validation test if the data values entered into a field do not match the field type

Example:

A data field with the number type will be rejected if nonnumeric data is entered

Fields with email and url types will be rejected if a user provides an invalid e-mail address or text that does not match the format of a URL

66

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

66

Testing for a Valid Pattern

To test whether a field value follows a valid pattern of characters, test the character string against a regular expression

Regular expression or regex is a concise description of a character pattern

To validate a text value against a regular expression, add the pattern attribute to the input element

67

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

67

Testing for a Valid Pattern (continued)

Example: The value of the custZip field against the regular expression pattern ^\d{5}$

<input name=“custZip” pattern=“^\d{5}$” />

where regular expression ^\d{5}$ represents any string of five numeric characters

68

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

68

Defining the Length of the Field Value

The syntax to define the maxlength attribute is <input maxlength=“value” />

where value is the maximum number of characters in the field value

Example:

<input name=”custZip” maxlength=“5” />

The maxlength attribute does not distinguish between characters and digits

69

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

69

Applying Inline Validation

Inline validation: The technique of immediate data validation and reporting of errors

The focus pseudo-class is used to change the display style of fields that currently contain invalid data

Focus: The state in which an element has been clicked by the user, making it the active control on the form

70

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

70

Applying Inline Validation (continued 1)

71

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

71

Applying Inline Validation (continued 2)

Example: To create styles for all of the checked option buttons in the form, apply the checked pseudo-class

input[type=”radio”]:checked {

styles

}

styles are the CSS styles applied to checked option buttons

72

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

72

Pseudo-Classes for Valid and Invalid Data

The valid and invalid pseudo-classes are used to format controls based on whether their field values pass/fail a validation test

Example: To display input elements containing valid data with a light green background, use the following style rule:

input:valid {

background-color: rgb(220, 255, 220);

}

73

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

73

Pseudo-Classes for Valid and Invalid Data (continued 1)

Example: To display input elements containing invalid data with a light red background with focus, use the following style rule:

input:focus:invalid {

background-color: rgb(255, 232, 233);

}

74

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

74

Pseudo-Classes for Valid and Invalid Data (continued 2)

75

New Perspectives on HTML5, CSS3, and JavaScript, 6th Edition

XP

XP

XP

XP

XP

75