SlideShare a Scribd company logo
1 of 44
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 ch10Terry Yoast
 
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 BasicsPramod Singla
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced FunctionsWebStackAcademy
 
12. session 12 java script objects
12. session 12   java script objects12. session 12   java script objects
12. session 12 java script objectsPhúc Đỗ
 
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 VariablesPramod 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_DOMJalpesh 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 TransformationsPramod Singla
 
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 TransformationsPramod 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 formsDr. 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 formDr. Ahmed Al Zaidy
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04Vivek chan
 
Maximizer 2018 API training
Maximizer 2018 API trainingMaximizer 2018 API training
Maximizer 2018 API trainingMurylo Batista
 
XPages Workshop: Concepts And Exercises
XPages Workshop:   Concepts And ExercisesXPages Workshop:   Concepts And Exercises
XPages Workshop: Concepts And Exercisesddrschiw
 
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 BackendBackand 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
 
TachVault's SaaS Derivative Options pricing
TachVault's SaaS Derivative Options pricingTachVault's SaaS Derivative Options pricing
TachVault's SaaS Derivative Options pricingNathan Muruganantha
 
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.0Axios 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 BeyondSalesforce Developers
 
Quick Test Professional (QTP/UFT)
Quick Test Professional (QTP/UFT)Quick Test Professional (QTP/UFT)
Quick Test Professional (QTP/UFT)Rajathi-QA
 
Html Server Input Checkbox Control CS
Html Server Input Checkbox Control CSHtml Server Input Checkbox Control CS
Html Server Input Checkbox Control CSsunmitraeducation
 

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
 
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
 
Html Server Input Checkbox Control CS
Html Server Input Checkbox Control CSHtml Server Input Checkbox Control CS
Html Server Input Checkbox Control CS
 

More from Terry Yoast

9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 
9781337102087 ppt ch10
9781337102087 ppt ch109781337102087 ppt ch10
9781337102087 ppt ch10Terry Yoast
 
9781337102087 ppt ch09
9781337102087 ppt ch099781337102087 ppt ch09
9781337102087 ppt ch09Terry Yoast
 
9781337102087 ppt ch08
9781337102087 ppt ch089781337102087 ppt ch08
9781337102087 ppt ch08Terry Yoast
 
9781337102087 ppt ch06
9781337102087 ppt ch069781337102087 ppt ch06
9781337102087 ppt ch06Terry Yoast
 
9781337102087 ppt ch05
9781337102087 ppt ch059781337102087 ppt ch05
9781337102087 ppt ch05Terry Yoast
 
9781337102087 ppt ch04
9781337102087 ppt ch049781337102087 ppt ch04
9781337102087 ppt ch04Terry Yoast
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10Terry Yoast
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09Terry Yoast
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08Terry Yoast
 
9780538745840 ppt ch07
9780538745840 ppt ch079780538745840 ppt ch07
9780538745840 ppt ch07Terry 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

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 

Recently uploaded (20)

Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 

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