SlideShare a Scribd company logo
JavaScript, Sixth Edition
Chapter 6
Enhancing and Validating Forms
JavaScript, Sixth Edition 2
Objectives
When you complete this chapter, you will be able to:
• Enhance form usability with JavaScript
• Customize browser-based HTML validation
• Implement custom validation to check for errors and
display error messages
JavaScript, Sixth Edition 3
Using JavaScript with Forms
• Validation
– checking that form information provided by users
conforms to data rules
• form object
– Represents a form in an HTML document
– Used to access form and its data
JavaScript, Sixth Edition 4
Table 6-1 Properties of form objects
Using JavaScript with Forms (cont’d.)
Table 6-3 Methods of form objects
Table 6-2 Event of form objects
JavaScript, Sixth Edition 5
Using JavaScript with Forms (cont'd.)
• Common elements for collecting form data:
– input
– select
– option
– textarea
– button
JavaScript, Sixth Edition 6
Table 6-4 Properties of elements within forms
Working with Input Fields (cont’d.)
JavaScript, Sixth Edition 7
Table 6-5 Methods of elements within forms
Working with Input Fields (cont’d.)
Table 6-6 Events of elements within forms
JavaScript, Sixth Edition 8
Referencing Forms and Form Elements
• Can use getElementsByTagName() method:
getElementsByTagName("form")[0]
• Document object includes a forms[] array
– Contains all forms on a web page
• form object has an elements[] array
JavaScript, Sixth Edition 9
Referencing Forms and Form Elements
(cont’d.)
• elements[] array
– Contains objects representing each control in a form
• Reference form index number in the forms[] array
– Followed by the appropriate element index number
from the elements[] array
JavaScript, Sixth Edition 10
Improving Form Usability
• Before validation
– Can reduce amount of validation necessary
JavaScript, Sixth Edition 11
Designing Forms to Collect More
Accurate Content
• Replace input boxes with other fields that present
limited choices
JavaScript, Sixth Edition 12
Designing Forms to Collect More
Accurate Content (cont'd.)
Table 6-7 Selected form elements for providing limited choices
JavaScript, Sixth Edition 13
Designing Forms to Collect More
Accurate Content (cont'd.)
Figure 6-3 Sample fieldset updated with option buttons and selection lists
Figure 6-2 Sample fieldset containing input elements
JavaScript, Sixth Edition 14
Programming Forms to Increase
Content Accuracy
• Assistive functions
– Reduce likelihood of user errors
– Prevent users from entering erroneous data
• Removing default values from selection lists
– Can set default value for selection list in HTML
• Only to one of the options
– JavaScript can set selectedIndex property to -1
• Corresponds to no selection
JavaScript, Sixth Edition 15
Programming Forms to Increase
Content Accuracy (cont'd.)
Table 6-8 select element properties
JavaScript, Sixth Edition 16
Programming Forms to Increase
Content Accuracy (cont'd.)
• Dynamically Updating Selection List Values
– Can add or remove option elements from a select
element using node methods
• Can change list options based on selection in another
field
JavaScript, Sixth Edition 17
Programming Forms to Increase
Content Accuracy (cont'd.)
Table 6-9 Properties of option elements
JavaScript, Sixth Edition 18
Programming Forms to Increase
Content Accuracy (cont'd.)
Figure 6-5 Diagram of function for dynamically updating selection list values
JavaScript, Sixth Edition 19
Programming Forms to Increase
Content Accuracy (cont'd.)
• Adding Placeholder Text for Older Browsers
– placeholder attribute of input and textarea
elements
• Supported by modern browsers
• Can recreate behavior with JavaScript for older
browsers:
– Add placeholder text when page finishes loading
– Remove placeholder text when user selects field
– Add back placeholder text if user makes no entry
JavaScript, Sixth Edition 20
Programming Forms to Increase
Content Accuracy (cont'd.)
• Automatically updating an associated field based on
a user entry
– Multiple elements may be associated
• Example: check box to indicate textarea entry
– Can automatically change value of one field in
response to change in other field
JavaScript, Sixth Edition 21
Programming Forms to Increase
Content Accuracy (cont'd.)
• Transferring duplicate field values
– Can copy data from one field to another based on
user indicating they should have the same value
• Example: Shipping Address and Billing Address
JavaScript, Sixth Edition 22
Programming Forms to Increase
Content Accuracy (cont'd.)
Figure 6-10 Billing Address entries copied to Delivery Address section
JavaScript, Sixth Edition 23
Customizing Browser-Based Validation
• Modern browsers can perform some validation
– Known as browser-based validation, native validation,
or HTML5 validation
JavaScript, Sixth Edition 24
Customizing Browser-Based Validation
(cont'd.)
• Specifying browser-based validation parameters
– Use attributes listed in Table 6-12
Table 6-12 HTML attributes to set browser-based validation parameters
JavaScript, Sixth Edition 25
Customizing Browser-Based Validation
(cont'd.)
• Specifying browser-based validation parameters
– Additional validation linked to input type values
Table 6-13 Values for type attribute that trigger browser-based validation
JavaScript, Sixth Edition 26
Customizing Browser-Based Validation
(cont'd.)
• Customizing browser-based validation feedback
– Modern browsers display feedback in similar ways,
with variation
• Displayed after submit event triggered
• Invalid controls highlighted
• Bubble displayed next to first control
JavaScript, Sixth Edition 27
Customizing Browser-Based Validation
(cont'd.)
• Customizing browser-based validation feedback
(cont'd.)
– Customizable through constraint validation API
• All properties of validity object must have value of
false for element to be valid
JavaScript, Sixth Edition 28
Customizing Browser-Based Validation
(cont'd.)
Table 6-14 validity properties
JavaScript, Sixth Edition 29
Customizing Browser-Based Validation
(cont'd.)
• Customizing browser-based validation feedback
(cont'd.)
– checkValidity() and setCustomValidity()
methods
– CSS :invalid and :valid pseudo-classes
• Use to change properties of form elements based on
validity status
JavaScript, Sixth Edition 30
Customizing Browser-Based Validation
(cont'd.)
• Customizing browser-based validation feedback
(cont'd.)
var fname = document.getElementbyId("firstName");
if (fname.valueMissing) {
setCustomValidity("Please fill out this field.");
}
#firstName:invalid {
background: rgb(255,233,233);
}
JavaScript
CSS
JavaScript, Sixth Edition 31
Customizing Browser-Based Validation
(cont'd.)
• Customizing browser-based validation feedback
(cont'd.)
Figure 6-13 Customized browser-based validation
JavaScript, Sixth Edition 32
Customizing Browser-Based Validation
(cont'd.)
• Customizing browser-based validation feedback
(cont'd.)
– Bubble appearance varies among browsers
– Cannot set multiple validation messages for a single
field at once
– Can disable browser-based validation using the
preventDefault() method and the invalid
event
• If disabled, must program custom validation
JavaScript, Sixth Edition 33
Programming Custom Validation
• Common validation functions:
– Checking that required fields contain entries
– Checking values dependent on other fields
– Checking for appropriate content type
JavaScript, Sixth Edition 34
Validating Submitted Data
• submit event fires when a form is submitted
– Often when submit button selected
– Data usually validated when submit event fires
– preventDefault() method disables default
behavior of an event when it fires
• Not supported in IE8, so set returnValue to false
instead
JavaScript, Sixth Edition 35
Validating Required Fields with Custom
Functions
• Retrieve values of required fields, then check if any
is empty
JavaScript, Sixth Edition 36
Validating Required Fields with Custom
Functions (cont’d.)
• Checking for empty text input fields
– Check value property for a value
– Use loop statement to check each field in a group
if (document.getElementById("firstName").value === "") {
// code to run if the field is blank
}
JavaScript, Sixth Edition 37
Validating Required Fields with Custom
Functions (cont’d.)
• Checking for selection lists with no values
– Check value of selectedIndex property
• If no option is selected, value is -1
if (document.getElementById("state").selectedIndex === -1 {
// code to run if the field is blank
}
JavaScript, Sixth Edition 38
Validating Required Fields with Custom
Functions (cont’d.)
• Checking for option button sets with no selection
– Check value of checked property
– Use And (&&) operators to check if no option button is
selected
var buttons = document.getElementsByName("Color");
if (!buttons[0].checked && !buttons[1].checked && ↵
!buttons[2].checked) {
// code to run if no button is selected
}
JavaScript, Sixth Edition 39
Validating Dependent Fields with
Custom Functions
• Sometimes need to test logic specific to a form
• Validating based on the state of a check box
– Access same checked property used with option
button
• Validating based on text input box contents
– Can use nested if statements to account for
possibilities when entry in one text box requires entry
in another
JavaScript, Sixth Edition 40
Validating Dependent Fields with
Custom Functions (cont'd.)
Figure 6-21 Flowchart diagram of validateCreateAccount() function
JavaScript, Sixth Edition 41
Validating Content Type with Custom
Functions
• Can check whether numeric fields contain numbers
– Use isNaN() function
• returns true if value is not a number
isNaN(document.getElementById("subtotal").value)
• Character patterns like zip codes require regular
expressions (Chapter 8)
JavaScript, Sixth Edition 42
Summary
• Validation checks that information conforms to rules
• Assistive functions reduce likelihood of user errors
• Browser-based validation is built into modern
browsers
– Customizable through Constraint Validation API
• preventDefault() method blocks action
normally associated with an event
JavaScript, Sixth Edition 43
Summary (cont’d.)
• To validate required text input fields
– Retrieve the values of the required fields
– Check if the value of any of them is an empty string
• To validate required selection lists
– Retrieve the selectedIndex value
– Check whether it’s equal to -1
JavaScript, Sixth Edition 44
Summary (cont’d.)
• To check if an option button is selected, access the
value of its checked property.
• To check if none of the option buttons in a set are
selected, create a conditional statement using And
(&&) operators
• In some cases, you need to create validation
functions to test logic specific to your form

More Related Content

What's hot

9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
Terry Yoast
 
javaScript and jQuery
javaScript and jQueryjavaScript and jQuery
javaScript and jQuery
Mehrab Hossain
 
Calypso browser
Calypso browserCalypso browser
Calypso browser
Denis Kudryashov
 
5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow Basics5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow Basics
Pramod Singla
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
WebStackAcademy
 
12. session 12 java script objects
12. session 12   java script objects12. session 12   java script objects
12. session 12 java script objectsPhúc Đỗ
 
SQL - RDBMS Concepts
SQL - RDBMS ConceptsSQL - RDBMS Concepts
SQL - RDBMS Concepts
WebStackAcademy
 
4\9 SSIS 2008R2_Training - Expression and Variables
4\9 SSIS 2008R2_Training - Expression and Variables4\9 SSIS 2008R2_Training - Expression and Variables
4\9 SSIS 2008R2_Training - Expression and Variables
Pramod Singla
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.netTarun Jain
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOM
Jalpesh Vasa
 
6.2\9 SSIS 2008R2_Training - DataFlow Transformations
6.2\9 SSIS 2008R2_Training - DataFlow Transformations6.2\9 SSIS 2008R2_Training - DataFlow Transformations
6.2\9 SSIS 2008R2_Training - DataFlow Transformations
Pramod Singla
 
For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
Snehal Harawande
 
Ajax
AjaxAjax
Ajax
Yoga Raja
 
Lecture 6. ADO.NET Overview.
Lecture 6. ADO.NET Overview.Lecture 6. ADO.NET Overview.
Lecture 6. ADO.NET Overview.
Alexey Furmanov
 
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
Pramod Singla
 

What's hot (18)

9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
javaScript and jQuery
javaScript and jQueryjavaScript and jQuery
javaScript and jQuery
 
Calypso browser
Calypso browserCalypso browser
Calypso browser
 
5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow Basics5\9 SSIS 2008R2_Training - DataFlow Basics
5\9 SSIS 2008R2_Training - DataFlow Basics
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
 
12. session 12 java script objects
12. session 12   java script objects12. session 12   java script objects
12. session 12 java script objects
 
Ch 7 data binding
Ch 7 data bindingCh 7 data binding
Ch 7 data binding
 
SQL - RDBMS Concepts
SQL - RDBMS ConceptsSQL - RDBMS Concepts
SQL - RDBMS Concepts
 
4\9 SSIS 2008R2_Training - Expression and Variables
4\9 SSIS 2008R2_Training - Expression and Variables4\9 SSIS 2008R2_Training - Expression and Variables
4\9 SSIS 2008R2_Training - Expression and Variables
 
For Beginners - Ado.net
For Beginners - Ado.netFor Beginners - Ado.net
For Beginners - Ado.net
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOM
 
6.2\9 SSIS 2008R2_Training - DataFlow Transformations
6.2\9 SSIS 2008R2_Training - DataFlow Transformations6.2\9 SSIS 2008R2_Training - DataFlow Transformations
6.2\9 SSIS 2008R2_Training - DataFlow Transformations
 
For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
 
Linq
LinqLinq
Linq
 
Ajax
AjaxAjax
Ajax
 
Ajax
AjaxAjax
Ajax
 
Lecture 6. ADO.NET Overview.
Lecture 6. ADO.NET Overview.Lecture 6. ADO.NET Overview.
Lecture 6. ADO.NET Overview.
 
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations6.1\9 SSIS 2008R2_Training - DataFlow Transformations
6.1\9 SSIS 2008R2_Training - DataFlow Transformations
 

Similar to 9781305078444 ppt ch06

Chapter 13 Programming for web forms
Chapter 13 Programming for web formsChapter 13 Programming for web forms
Chapter 13 Programming for web forms
Dr. Ahmed Al Zaidy
 
Chapter 7 Designing a web form
Chapter 7 Designing a web formChapter 7 Designing a web form
Chapter 7 Designing a web form
Dr. Ahmed Al Zaidy
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04
Vivek chan
 
Maximizer 2018 API training
Maximizer 2018 API trainingMaximizer 2018 API training
Maximizer 2018 API training
Murylo Batista
 
Asp PPT (.NET )
Asp PPT (.NET )Asp PPT (.NET )
Asp PPT (.NET )
Ankit Gupta
 
XPages Workshop: Concepts And Exercises
XPages Workshop:   Concepts And ExercisesXPages Workshop:   Concepts And Exercises
XPages Workshop: Concepts And Exercises
ddrschiw
 
How to Build Dynamic Forms in Angular Directive with a Backend
How to Build Dynamic Forms in Angular Directive with a BackendHow to Build Dynamic Forms in Angular Directive with a Backend
How to Build Dynamic Forms in Angular Directive with a Backend
Backand Cohen
 
Web topic 22 validation on web forms
Web topic 22  validation on web formsWeb topic 22  validation on web forms
Web topic 22 validation on web formsCK Yang
 
validation & regular expression chacteristics
validation & regular expression chacteristicsvalidation & regular expression chacteristics
validation & regular expression chacteristics
L21IT131DRajkumar
 
data binding.docx
data binding.docxdata binding.docx
data binding.docx
kishorebabu123
 
TachVault's SaaS Derivative Options pricing
TachVault's SaaS Derivative Options pricingTachVault's SaaS Derivative Options pricing
TachVault's SaaS Derivative Options pricing
Nathan Muruganantha
 
Free Trial: How to access Options Pricing?
Free Trial: How to access Options Pricing?Free Trial: How to access Options Pricing?
Free Trial: How to access Options Pricing?
Muruganantha Nadesapillai
 
ASP.NET Session 10
ASP.NET Session 10ASP.NET Session 10
ASP.NET Session 10Sisir Ghosh
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04Mani Chaubey
 
Axios Systems assyst RUG2017 - Personalisation of assyst v2.0
Axios Systems assyst RUG2017 - Personalisation of assyst v2.0Axios Systems assyst RUG2017 - Personalisation of assyst v2.0
Axios Systems assyst RUG2017 - Personalisation of assyst v2.0
Axios Systems
 
Test strategy for web development
Test strategy for web developmentTest strategy for web development
Test strategy for web developmentalice yang
 
Taking Apex and Visualforce Above and Beyond
Taking Apex and Visualforce Above and BeyondTaking Apex and Visualforce Above and Beyond
Taking Apex and Visualforce Above and Beyond
Salesforce Developers
 
Quick Test Professional (QTP/UFT)
Quick Test Professional (QTP/UFT)Quick Test Professional (QTP/UFT)
Quick Test Professional (QTP/UFT)
Rajathi-QA
 
OpenCms Days 2015 Advanced Solr Searching
OpenCms Days 2015 Advanced Solr SearchingOpenCms Days 2015 Advanced Solr Searching
OpenCms Days 2015 Advanced Solr Searching
Alkacon Software GmbH & Co. KG
 

Similar to 9781305078444 ppt ch06 (20)

Chapter 13 Programming for web forms
Chapter 13 Programming for web formsChapter 13 Programming for web forms
Chapter 13 Programming for web forms
 
Chapter 7 Designing a web form
Chapter 7 Designing a web formChapter 7 Designing a web form
Chapter 7 Designing a web form
 
Chapter09
Chapter09Chapter09
Chapter09
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04
 
Maximizer 2018 API training
Maximizer 2018 API trainingMaximizer 2018 API training
Maximizer 2018 API training
 
Asp PPT (.NET )
Asp PPT (.NET )Asp PPT (.NET )
Asp PPT (.NET )
 
XPages Workshop: Concepts And Exercises
XPages Workshop:   Concepts And ExercisesXPages Workshop:   Concepts And Exercises
XPages Workshop: Concepts And Exercises
 
How to Build Dynamic Forms in Angular Directive with a Backend
How to Build Dynamic Forms in Angular Directive with a BackendHow to Build Dynamic Forms in Angular Directive with a Backend
How to Build Dynamic Forms in Angular Directive with a Backend
 
Web topic 22 validation on web forms
Web topic 22  validation on web formsWeb topic 22  validation on web forms
Web topic 22 validation on web forms
 
validation & regular expression chacteristics
validation & regular expression chacteristicsvalidation & regular expression chacteristics
validation & regular expression chacteristics
 
data binding.docx
data binding.docxdata binding.docx
data binding.docx
 
TachVault's SaaS Derivative Options pricing
TachVault's SaaS Derivative Options pricingTachVault's SaaS Derivative Options pricing
TachVault's SaaS Derivative Options pricing
 
Free Trial: How to access Options Pricing?
Free Trial: How to access Options Pricing?Free Trial: How to access Options Pricing?
Free Trial: How to access Options Pricing?
 
ASP.NET Session 10
ASP.NET Session 10ASP.NET Session 10
ASP.NET Session 10
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04
 
Axios Systems assyst RUG2017 - Personalisation of assyst v2.0
Axios Systems assyst RUG2017 - Personalisation of assyst v2.0Axios Systems assyst RUG2017 - Personalisation of assyst v2.0
Axios Systems assyst RUG2017 - Personalisation of assyst v2.0
 
Test strategy for web development
Test strategy for web developmentTest strategy for web development
Test strategy for web development
 
Taking Apex and Visualforce Above and Beyond
Taking Apex and Visualforce Above and BeyondTaking Apex and Visualforce Above and Beyond
Taking Apex and Visualforce Above and Beyond
 
Quick Test Professional (QTP/UFT)
Quick Test Professional (QTP/UFT)Quick Test Professional (QTP/UFT)
Quick Test Professional (QTP/UFT)
 
OpenCms Days 2015 Advanced Solr Searching
OpenCms Days 2015 Advanced Solr SearchingOpenCms Days 2015 Advanced Solr Searching
OpenCms Days 2015 Advanced Solr Searching
 

More from Terry Yoast

9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
Terry Yoast
 
9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10
Terry Yoast
 
9781337102087 ppt ch09
9781337102087 ppt ch099781337102087 ppt ch09
9781337102087 ppt ch09
Terry Yoast
 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08
Terry Yoast
 
9781337102087 ppt ch06
9781337102087 ppt ch069781337102087 ppt ch06
9781337102087 ppt ch06
Terry Yoast
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05
Terry Yoast
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04
Terry Yoast
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
Terry Yoast
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
Terry Yoast
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08
Terry Yoast
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07
Terry Yoast
 

More from Terry Yoast (20)

9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 
9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10
 
9781337102087 ppt ch09
9781337102087 ppt ch099781337102087 ppt ch09
9781337102087 ppt ch09
 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08
 
9781337102087 ppt ch06
9781337102087 ppt ch069781337102087 ppt ch06
9781337102087 ppt ch06
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07
 

Recently uploaded

CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 

Recently uploaded (20)

CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 

9781305078444 ppt ch06

  • 1. JavaScript, Sixth Edition Chapter 6 Enhancing and Validating Forms
  • 2. JavaScript, Sixth Edition 2 Objectives When you complete this chapter, you will be able to: • Enhance form usability with JavaScript • Customize browser-based HTML validation • Implement custom validation to check for errors and display error messages
  • 3. JavaScript, Sixth Edition 3 Using JavaScript with Forms • Validation – checking that form information provided by users conforms to data rules • form object – Represents a form in an HTML document – Used to access form and its data
  • 4. JavaScript, Sixth Edition 4 Table 6-1 Properties of form objects Using JavaScript with Forms (cont’d.) Table 6-3 Methods of form objects Table 6-2 Event of form objects
  • 5. JavaScript, Sixth Edition 5 Using JavaScript with Forms (cont'd.) • Common elements for collecting form data: – input – select – option – textarea – button
  • 6. JavaScript, Sixth Edition 6 Table 6-4 Properties of elements within forms Working with Input Fields (cont’d.)
  • 7. JavaScript, Sixth Edition 7 Table 6-5 Methods of elements within forms Working with Input Fields (cont’d.) Table 6-6 Events of elements within forms
  • 8. JavaScript, Sixth Edition 8 Referencing Forms and Form Elements • Can use getElementsByTagName() method: getElementsByTagName("form")[0] • Document object includes a forms[] array – Contains all forms on a web page • form object has an elements[] array
  • 9. JavaScript, Sixth Edition 9 Referencing Forms and Form Elements (cont’d.) • elements[] array – Contains objects representing each control in a form • Reference form index number in the forms[] array – Followed by the appropriate element index number from the elements[] array
  • 10. JavaScript, Sixth Edition 10 Improving Form Usability • Before validation – Can reduce amount of validation necessary
  • 11. JavaScript, Sixth Edition 11 Designing Forms to Collect More Accurate Content • Replace input boxes with other fields that present limited choices
  • 12. JavaScript, Sixth Edition 12 Designing Forms to Collect More Accurate Content (cont'd.) Table 6-7 Selected form elements for providing limited choices
  • 13. JavaScript, Sixth Edition 13 Designing Forms to Collect More Accurate Content (cont'd.) Figure 6-3 Sample fieldset updated with option buttons and selection lists Figure 6-2 Sample fieldset containing input elements
  • 14. JavaScript, Sixth Edition 14 Programming Forms to Increase Content Accuracy • Assistive functions – Reduce likelihood of user errors – Prevent users from entering erroneous data • Removing default values from selection lists – Can set default value for selection list in HTML • Only to one of the options – JavaScript can set selectedIndex property to -1 • Corresponds to no selection
  • 15. JavaScript, Sixth Edition 15 Programming Forms to Increase Content Accuracy (cont'd.) Table 6-8 select element properties
  • 16. JavaScript, Sixth Edition 16 Programming Forms to Increase Content Accuracy (cont'd.) • Dynamically Updating Selection List Values – Can add or remove option elements from a select element using node methods • Can change list options based on selection in another field
  • 17. JavaScript, Sixth Edition 17 Programming Forms to Increase Content Accuracy (cont'd.) Table 6-9 Properties of option elements
  • 18. JavaScript, Sixth Edition 18 Programming Forms to Increase Content Accuracy (cont'd.) Figure 6-5 Diagram of function for dynamically updating selection list values
  • 19. JavaScript, Sixth Edition 19 Programming Forms to Increase Content Accuracy (cont'd.) • Adding Placeholder Text for Older Browsers – placeholder attribute of input and textarea elements • Supported by modern browsers • Can recreate behavior with JavaScript for older browsers: – Add placeholder text when page finishes loading – Remove placeholder text when user selects field – Add back placeholder text if user makes no entry
  • 20. JavaScript, Sixth Edition 20 Programming Forms to Increase Content Accuracy (cont'd.) • Automatically updating an associated field based on a user entry – Multiple elements may be associated • Example: check box to indicate textarea entry – Can automatically change value of one field in response to change in other field
  • 21. JavaScript, Sixth Edition 21 Programming Forms to Increase Content Accuracy (cont'd.) • Transferring duplicate field values – Can copy data from one field to another based on user indicating they should have the same value • Example: Shipping Address and Billing Address
  • 22. JavaScript, Sixth Edition 22 Programming Forms to Increase Content Accuracy (cont'd.) Figure 6-10 Billing Address entries copied to Delivery Address section
  • 23. JavaScript, Sixth Edition 23 Customizing Browser-Based Validation • Modern browsers can perform some validation – Known as browser-based validation, native validation, or HTML5 validation
  • 24. JavaScript, Sixth Edition 24 Customizing Browser-Based Validation (cont'd.) • Specifying browser-based validation parameters – Use attributes listed in Table 6-12 Table 6-12 HTML attributes to set browser-based validation parameters
  • 25. JavaScript, Sixth Edition 25 Customizing Browser-Based Validation (cont'd.) • Specifying browser-based validation parameters – Additional validation linked to input type values Table 6-13 Values for type attribute that trigger browser-based validation
  • 26. JavaScript, Sixth Edition 26 Customizing Browser-Based Validation (cont'd.) • Customizing browser-based validation feedback – Modern browsers display feedback in similar ways, with variation • Displayed after submit event triggered • Invalid controls highlighted • Bubble displayed next to first control
  • 27. JavaScript, Sixth Edition 27 Customizing Browser-Based Validation (cont'd.) • Customizing browser-based validation feedback (cont'd.) – Customizable through constraint validation API • All properties of validity object must have value of false for element to be valid
  • 28. JavaScript, Sixth Edition 28 Customizing Browser-Based Validation (cont'd.) Table 6-14 validity properties
  • 29. JavaScript, Sixth Edition 29 Customizing Browser-Based Validation (cont'd.) • Customizing browser-based validation feedback (cont'd.) – checkValidity() and setCustomValidity() methods – CSS :invalid and :valid pseudo-classes • Use to change properties of form elements based on validity status
  • 30. JavaScript, Sixth Edition 30 Customizing Browser-Based Validation (cont'd.) • Customizing browser-based validation feedback (cont'd.) var fname = document.getElementbyId("firstName"); if (fname.valueMissing) { setCustomValidity("Please fill out this field."); } #firstName:invalid { background: rgb(255,233,233); } JavaScript CSS
  • 31. JavaScript, Sixth Edition 31 Customizing Browser-Based Validation (cont'd.) • Customizing browser-based validation feedback (cont'd.) Figure 6-13 Customized browser-based validation
  • 32. JavaScript, Sixth Edition 32 Customizing Browser-Based Validation (cont'd.) • Customizing browser-based validation feedback (cont'd.) – Bubble appearance varies among browsers – Cannot set multiple validation messages for a single field at once – Can disable browser-based validation using the preventDefault() method and the invalid event • If disabled, must program custom validation
  • 33. JavaScript, Sixth Edition 33 Programming Custom Validation • Common validation functions: – Checking that required fields contain entries – Checking values dependent on other fields – Checking for appropriate content type
  • 34. JavaScript, Sixth Edition 34 Validating Submitted Data • submit event fires when a form is submitted – Often when submit button selected – Data usually validated when submit event fires – preventDefault() method disables default behavior of an event when it fires • Not supported in IE8, so set returnValue to false instead
  • 35. JavaScript, Sixth Edition 35 Validating Required Fields with Custom Functions • Retrieve values of required fields, then check if any is empty
  • 36. JavaScript, Sixth Edition 36 Validating Required Fields with Custom Functions (cont’d.) • Checking for empty text input fields – Check value property for a value – Use loop statement to check each field in a group if (document.getElementById("firstName").value === "") { // code to run if the field is blank }
  • 37. JavaScript, Sixth Edition 37 Validating Required Fields with Custom Functions (cont’d.) • Checking for selection lists with no values – Check value of selectedIndex property • If no option is selected, value is -1 if (document.getElementById("state").selectedIndex === -1 { // code to run if the field is blank }
  • 38. JavaScript, Sixth Edition 38 Validating Required Fields with Custom Functions (cont’d.) • Checking for option button sets with no selection – Check value of checked property – Use And (&&) operators to check if no option button is selected var buttons = document.getElementsByName("Color"); if (!buttons[0].checked && !buttons[1].checked && ↵ !buttons[2].checked) { // code to run if no button is selected }
  • 39. JavaScript, Sixth Edition 39 Validating Dependent Fields with Custom Functions • Sometimes need to test logic specific to a form • Validating based on the state of a check box – Access same checked property used with option button • Validating based on text input box contents – Can use nested if statements to account for possibilities when entry in one text box requires entry in another
  • 40. JavaScript, Sixth Edition 40 Validating Dependent Fields with Custom Functions (cont'd.) Figure 6-21 Flowchart diagram of validateCreateAccount() function
  • 41. JavaScript, Sixth Edition 41 Validating Content Type with Custom Functions • Can check whether numeric fields contain numbers – Use isNaN() function • returns true if value is not a number isNaN(document.getElementById("subtotal").value) • Character patterns like zip codes require regular expressions (Chapter 8)
  • 42. JavaScript, Sixth Edition 42 Summary • Validation checks that information conforms to rules • Assistive functions reduce likelihood of user errors • Browser-based validation is built into modern browsers – Customizable through Constraint Validation API • preventDefault() method blocks action normally associated with an event
  • 43. JavaScript, Sixth Edition 43 Summary (cont’d.) • To validate required text input fields – Retrieve the values of the required fields – Check if the value of any of them is an empty string • To validate required selection lists – Retrieve the selectedIndex value – Check whether it’s equal to -1
  • 44. JavaScript, Sixth Edition 44 Summary (cont’d.) • To check if an option button is selected, access the value of its checked property. • To check if none of the option buttons in a set are selected, create a conditional statement using And (&&) operators • In some cases, you need to create validation functions to test logic specific to your form