Chapter 5 Validating Form Data with JavaScript

Slides:



Advertisements
Similar presentations
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
Advertisements

Tutorial 6 Creating a Web Form
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
JavaScript Forms Form Validation Cookies CGI Programs.
Creating Web Page Forms. Objectives Describe how Web forms can interact with a server-based program Insert a form into a Web page Create and format a.
Tutorial 6 Working with Web Forms
Forms Review. 2 Using Forms tag  Contains the form elements on a web page  Container tag tag  Configures a variety of form elements including text.
Forms Describe common uses of forms on web pages
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Creating Web Page Forms
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (NON-audio version) © Dr. David C. Gibbs
Tutorial 6 Forms Section A - Working with Forms in JavaScript.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
CST JavaScript Validating Form Data with JavaScript.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
4-Sep-15 HTML Forms Mrs. Goins Web Design Class. Parts of a Web Form A Form is an area that can contain Form Control/Elements. Each piece of information.
XP Tutorial 6New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Creating Web Page Forms Designing a Product Registration Form Tutorial.
XP Tutorial 6New Perspectives on HTML and XHTML, Comprehensive 1 Creating Web Page Forms Designing a Product Registration Form Tutorial 6.
1 Creating Web Forms in HTML Web forms collect information from customers Web forms include different control elements including: –Input boxes –Selection.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
© 2011 Delmar, Cengage Learning Chapter 9 Collecting Data with Forms.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
Copyright ©2005  Department of Computer & Information Science Introducing DHTML & DOM: JavaScript & Forms.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 6 1 Microsoft Office FrontPage 2003 Tutorial 6 – Publishing a Web Site.
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
© 2010 Delmar, Cengage Learning Chapter 8 Collecting Data with Forms.
Using Client-Side Scripts to Enhance Web Applications 1.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
HTML Forms.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
Dreamweaver MX. 2 Overview of Templates n Forms enable you to collect data from ______. n A form contains ________ such as text fields, radio buttons,
ITCS373: Internet Technology Lecture 5: More HTML.
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Microsoft FrontPage 2003 Illustrated Complete Creating a Form.
Forms Collecting Data CSS Class 5. Forms Create a form Add text box Add labels Add check boxes and radio buttons Build a drop-down list Group drop-down.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
HTML FORMS The TEXT Object Presented By: Ankit Gupta.
HTML Forms.
Chapter 9 Quick Links Slide 2 Performance Objectives Understanding Forms Planning Forms Creating Forms Creating Text Fields Creating Hidden Fields Creating.
Creating Forms on a Web Page. 2 Introduction  Forms allow Web developers to collect visitor feedback  Forms create an environment that invites people.
Copyright (c) 2004 Prentice-Hall. All rights reserved. 1 Committed to Shaping the Next Generation of IT Experts. Project 6: Creating XHTML Forms Kelly.
HTML III (Forms) Robin Burke ECT 270. Outline Where we are in this class Web applications HTML Forms Break Forms lab.
Ashima Wadhwa Java Script And Forms. Introduction Forms: –One of the most common Web page elements used with JavaScript –Typical forms you may encounter.
XP Tutorial 6New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Creating Web Page Forms Designing a Product Registration Form Tutorial 6.
2440: 141 Web Site Administration Web Forms Instructor: Joseph Nattey.
XHTML Forms.
Tutorial 6 Working with Web Forms
Section 10.1 Define scripting
JavaScript, Sixth Edition
In this session, you will learn to:
How to Write Web Forms By Mimi Opkins.
Section 17.1 Section 17.2 Add an audio file using HTML
ITE 115 Creating Web Page Forms
Basic XHTML Tables XHTML tables—a frequently used feature that organizes data into rows and columns. Tables are defined with the table element. Table.
FORM OBJECT When creating an interactive web site for the Internet it is necessary to capture user input and process this input. Based on the result of.
CNIT 131 HTML5 - Forms.
Web Development & Design Foundations with H T M L 5
Creating Forms on a Web Page
Chapter 6: Creating Web Page Forms. An Example of a Form.
Presentation transcript:

Chapter 5 Validating Form Data with JavaScript

Objectives Study form elements and objects Use JavaScript to manipulate and validate form elements Learn how to submit and reset forms Learn how to validate submitted form data JavaScript, Fourth Edition 2 2 JavaScript, Fourth Edition

Overview of Forms Many Web sites use forms Collect information from users and transmit to a server for processing Forms frequently found on Web pages gather search criteria from a user Data collected is transmitted to a server-side scripting language program on a Web server Popular server-side scripting languages PHP, Common Gateway Interface (CGI), Active Server Pages (ASP), and Java Server Pages (JSP) JavaScript, Fourth Edition

Overview of Forms (continued) JavaScript, Fourth Edition 4

Overview of Forms (continued) JavaScript, Fourth Edition 5

Understanding Form Elements and Objects Learn about the primary form elements and how to access them with JavaScript JavaScript, Fourth Edition

The <form> Element Designates a form within a Web page and contains all the text and elements that make up a form Can set up a form to send data to an e-mail address Replace the Web server script’s URL in the <form> element’s action attribute with the mailto protocol JavaScript, Fourth Edition

The <form> Element (continued) JavaScript, Fourth Edition 8

Form Controls Primary elements used within the <form> element <input>, <button>, <select>, and <textarea> <input> and <button> elements Create input fields with which users interact <select> element Displays choices in drop-down menu or scrolling list <textarea> element Create a text field in which users can enter multiple lines of information JavaScript, Fourth Edition 9

Form Controls (continued) Field Any form element into which a user can enter data or that a user can select or change <input>, <textarea>, and <select> elements can include name and value attributes name attribute defines a name for an element value attribute defines a default value JavaScript, Fourth Edition 10

Using JavaScript with Forms Form object Represents a form on a Web page Used in JavaScript to access form controls and verify form information Part of the browser object model Referencing Forms and Form Elements Document object includes a forms[] array that contains all the forms on a Web page The <form> element’s name attribute is deprecated in XHTML Form object has an elements[] array JavaScript, Fourth Edition 11

Using JavaScript with Forms (continued) Referencing Forms and Form Elements (continued) elements[] array Contains objects representing each control in a form Reference the index number of the form in the forms[] array Followed by the appropriate element index number from the elements[] array The Form Object See Tables 5-2, 5-3, and 5-4 Example: The Gosselin Gazette Web page JavaScript, Fourth Edition 12

Using JavaScript with Forms (continued) JavaScript, Fourth Edition 13

Using JavaScript with Forms (continued) JavaScript, Fourth Edition 14

Working with Input Fields Empty <input> element Generate input fields that create interface elements Such as text boxes, radio buttons, and so on Minimized form When a Boolean attribute is not assigned a value Illegal in XHTML Full form of a Boolean attribute Created by assigning the name of the attribute itself as the attribute’s value JavaScript, Fourth Edition 15

Working with Input Fields (continued) JavaScript, Fourth Edition 16

Input Field Objects For controls created with an <input> element Each control is represented by an object that is similar to the name of the control Input Radio Checkbox JavaScript, Fourth Edition 17

Input Field Objects (continued) JavaScript, Fourth Edition 18

Input Field Objects (continued) JavaScript, Fourth Edition 19

Text Boxes Text box value attribute An <input> element with a type of “text” Accepts a single line of text value attribute Specifies text to be used as the default value at the moment a form first loads Example: The Gosselin Gazette Web page Add text <input> elements to the Subscription form to collect basic customer data JavaScript, Fourth Edition 20

Text Boxes (continued) JavaScript, Fourth Edition 21

Text Boxes (continued) Most form validation with JavaScript takes place when you submit the form You can use JavaScript’s built-in isNaN() function Determines if value entered by the user is a number Example: The Gosselin Gazette Web page Add function to Subscription.html JavaScript, Fourth Edition 22

Password Boxes Password box Example: The Gosselin Gazette Web page An <input> element with a type of “password” Entering passwords or other types of sensitive data Character typed appears as an asterisk or bullet Example: The Gosselin Gazette Web page Add a password <input> element to Subscription.html JavaScript, Fourth Edition

Password Boxes (continued) JavaScript, Fourth Edition 24

Push Buttons Push button Example An <input> element with a type of “button” Similar to OK and Cancel buttons in dialog boxes Primary purpose is to execute JavaScript code Example <p><input type="button“ name="push_button" value="Click Here" onclick="window.alert('You clicked a push button.');" /></p> JavaScript, Fourth Edition 25

Push Buttons (continued) JavaScript, Fourth Edition 26

Radio Buttons Group of radio buttons, or option buttons An <input> element with a type of “radio” User can select only one value All radio buttons in the group must have the same name attribute Each radio button requires a value attribute that identifies its unique value checked attribute in a radio <input> element Sets an initial value for the group Example: The Gosselin Gazette Web page JavaScript, Fourth Edition 27

Radio Buttons (continued) JavaScript, Fourth Edition 28

Radio Buttons (continued) When multiple form elements share same name JavaScript creates an array out of the elements using the shared name Radio buttons share the same name A single name=value pair can be submitted to a server-side script checked property returns a value of true if a check box or radio button is selected Example: The Gosselin Gazette Web page Add more radio buttons to the subscription form JavaScript, Fourth Edition 29

Check Boxes Check boxes checked attribute An <input> element with a type of “checkbox” Can be set to Yes (checked) or No (unchecked) When you want users to select whether or not to include a certain item Or to allow users to select multiple values from a list checked attribute Sets the initial value of the check box to Yes Group check boxes by giving each check box the same name value JavaScript, Fourth Edition 30

Check Boxes (continued) JavaScript, Fourth Edition 31

Check Boxes (continued) Each check box can have a different value Users can select as many check boxes in a group as they like Example: The Gosselin Gazette Web page Add check boxes to the Subscription.html document to allow users to select any other newspapers Use a check box element in Billing Information and Shipping Information at the top of the form in Subscription.html JavaScript, Fourth Edition 32

Creating Selection Lists The <select> element creates a selection list Presents users with fixed lists of options Options displayed in a selection list are created with <option> elements <select> element must appear within a block-level element such as the <p> element A selection list can also include a scroll bar JavaScript, Fourth Edition

Creating Selection Lists (continued) JavaScript, Fourth Edition 34

Menu Options <option> element Specifies the options that appear in a selection list Each selection list must contain at least one <option> element Example: The Gosselin Gazette Web page Add a selection list to Subscription.html Subscriber uses to select any magazines to which they are currently subscribed JavaScript, Fourth Edition 35

Menu Options (continued) JavaScript, Fourth Edition 36

The Select and Option Objects Select object Represents a selection list in a form Includes an options[] array containing an Option object for each <option> element in the selection list Option object Represents an option in a selection list JavaScript, Fourth Edition

The Select and Option Objects (continued) JavaScript, Fourth Edition 38

The Select and Option Objects (continued) JavaScript, Fourth Edition 39

Adding Options to a Selection List ECMAScript recommendations suggest using the add() method of the Select object To add new options to a selection list Method is not consistently implemented Create a new option with Option() constructor Then, assign the object to an empty element in an options[] array Example Add a selection list to Subscription.html JavaScript, Fourth Edition

Removing Options from a Selection List Pass option’s index number in options[] array to the remove() method of the Select object Remaining elements are reordered Remove all the options from an options array Set length of options[] array to zero Example Add code to Subscription.html that deletes magazine names from the selection list JavaScript, Fourth Edition 41

Changing Options in a Selection List Assign new values to the option’s value and text properties Example: The Gosselin Gazette Web Page Add code to Subscription.html that modifies magazine names in the selection list JavaScript, Fourth Edition

Submitting and Resetting Forms Learn how to submit forms to a server-side script And how to reset form fields to their default values Use JavaScript to: Validate submitted data Confirm whether users really want to reset form fields JavaScript, Fourth Edition

Submit Buttons Submit button An <input> element with a type of “submit” Transmits a form’s data to a Web server action attribute of the <form> element Determines to what URL the form is submitted Submit buttons do not have values that are submitted to a Web server Image submit button An <input> element with a type of “image” Displays a graphical image and transmits a form’s data to a Web server JavaScript, Fourth Edition 44

Submit Buttons (continued) Image submit button (continued) Include the src attribute to specify the image to display on the button Example: The Gosselin Gazette Web page Add a submit button to Subscription.html JavaScript, Fourth Edition 45

Submit Buttons (continued) JavaScript, Fourth Edition 46

Reset Buttons Reset button An <input> element with a type of “reset” Clears all form entries and resets each form element to the initial value specified by its value attribute Text you assign to the reset button’s value attribute appears as the button label Example: The Gosselin Gazette Web page Add a reset button to Subscription.html JavaScript, Fourth Edition 47

Validating Submitted Data onsubmit event handler Executes when a form is submitted to a server- side script Often used to verify or validate a form’s data before it is sent to a server onreset event handler Executes when a reset button is selected on a form Confirm that a user really wants to reset the contents of a form JavaScript, Fourth Edition 48

Validating Submitted Data (continued) Must return a value of true or false Depending on whether the form should be submitted (true) or reset (false) Example: Gosselin Gazette Web page Add onsubmit and onreset event handlers to Subscription.html JavaScript, Fourth Edition 49

Validating Text and Password Boxes Use an if statement in the onsubmit event handler Check whether field’s value property contains a value Example: The Gosselin Gazette Web page Add code to the confirmSubmit() function in the Subscription form that validates the text and password boxes JavaScript, Fourth Edition 50

Validating Radio Buttons Use the checked property to determine which element in a group is selected Example: The Gosselin Gazette Web page Add code to the confirmSubmit() function in the Subscription form that validates the Delivery Rates radio buttons JavaScript, Fourth Edition

Validating Check Boxes Use the checked property to determine whether an individual check box has been selected JavaScript, Fourth Edition 52

Validating Selection Lists Test whether the selection list’s selectedIndex property contains a value of -1 If it does, then no option is selected Example: The Gosselin Gazette Web page Add code to the Subscription form that selects all of the magazines when the form is submitted JavaScript, Fourth Edition 53

Summary Forms collect information from users and transmit that information to a server for processing <form> element designates a form in a Web page Elements to create form controls: <input>, <button>, <select>, and <textarea> Any form element into which a user can enter data is called a field The Form object represents a form on a Web page JavaScript, Fourth Edition

Summary (continued) Document object includes a forms[] array that contains all of the forms on a Web page Empty <input> element is used to generate input fields <select> element creates a selection list Use <option> elements to specify the options that appear in a selection list Select object represents a selection list in a form JavaScript, Fourth Edition

Summary (continued) Option object represents an option in a selection list Submit button transmits a form’s data to a Web server A reset button clears all form entries and resets each form element to the initial value specified by its value attribute onsubmit event handler executes when a form is submitted to a server-side script onreset event handler executes when a reset button is selected on a form JavaScript, Fourth Edition 56