1 lesson 8 creating forms with javascript html and javascript basics, 4 th edition barksdale /...

16
1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Upload: william-white

Post on 24-Dec-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

1

Lesson 8Creating Forms with JavaScript

HTML and JavaScript BASICS, 4th Edition

Barksdale / Turner

Page 2: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E22

Objectives

Create an HTML form. Make a form submission button functional. Make a form clearing button functional. Validate text fields with JavaScript. Validate radio buttons with JavaScript.

Page 3: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E33

Vocabulary

check box components controls data validation radio button text field

Page 4: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E44

Making HTML Forms More Functional

Start by defining the general layout of the form on a Web page using HTML tags.

Identify the various objects in the HTML form with which the users interact.

Name each of these interactive objects (also called controls or components).

Write the JavaScript functions invoked when the user triggers a specific JavaScript event.

Page 5: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E55

Making HTML Forms More Functional (continued)

Data validation is the process of checking user input data to make sure that it is complete and accurate.

There are many kinds of mistakes, such as missing data, which can be detected by JavaScript.

Page 6: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E66

Creating a Pizza Order Form

The first step in creating an effective order form for the JavaScript Pizza Parlor is to define its appearance on the Web page with the appropriate HTML tags.

Pay close attention to the name attribute used in the HTML tags. Although the names do not affect the form appearance, they are critical elements of the document.

Page 7: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E77

Creating a Pizza Order Form (continued)

Text field: Input control that allows the user to enter a string value into a specific location on the Web page.

Radio button (option button): Input control that allows the user to select just one option from a set of options.

Check box: Input control that allows the user to select any number of options from a set of options.

Page 8: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E88

Creating a Pizza Order Form (continued)

Pizza Order Form

You can use text fields, radio buttons, and check boxes within the same form.

Page 9: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E99

Creating a Pizza Order Form (continued)

A string is a sequence of one or more characters, and can consist of a single word, a complete sentence, or an entire chapter of a book.

Normally, strings contain meaningful text, but this is not a requirement.

Page 10: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E1010

Making the Submit Order Button Functional

To start, add a JavaScript function that will be called when the user clicks the Submit Order button. In this case, you enter a doSubmit() function that invokes the alert() method.

To get the Submit Order button to call a JavaScript function when it is clicked, you use an event called onClick.

Page 11: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E1111

Making the Submit Order Button Functional (continued)

It is important to give users feedback and to let them know their order has been processed.

Web page with Submit Order button clicked

Page 12: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E1212

Making the Clear Entries Button Functional

You can build features into forms to make it easy for people to correct their errors.

Add the doClear() function. Add an onClick event to the <input> tag of the

Clear Entries button. This event will call the doClear() function to erase any existing form data.

Page 13: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E1313

Validating Text Fields

The validateText() function can only return one of two possible values: false or true.

The false value is returned if the value property of an object is an empty string.

If none of the required text fields are blank, meaning a user entered something in each of the fields, the function returns the true value.

Page 14: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E1414

Validating Text Fields (continued)

The DoSubmit() function evaluates the validateText() function and displays an alert box if information is missing.

Data validation – text fields

Page 15: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E1515

Validating Radio Buttons

To validate a set of radio buttons, you need to test the value of each object’s checked property to see if it is set to true or false.

The validateRadio() function returns a true value if it encounters a selected radio button.

However, if none of the radio buttons in the set is selected, the function returns a false value.

Page 16: 1 Lesson 8 Creating Forms with JavaScript HTML and JavaScript BASICS, 4 th Edition Barksdale / Turner

Le

sso

n 8

Barksdale / Turner HTML and JavaScript BASICS 4E1616

Summary

In this lesson, you learned: How to create an HTML form. How to make a form submission button

functional. How to make a form clearing button functional. How to validate text fields with JavaScript. How to validate radio buttons with JavaScript.