Download presentation
Presentation is loading. Please wait.
Published byEthan Parks Modified over 9 years ago
1
Modern JavaScript Develop And Design Instructor’s Notes Chapter 10 – Working with Forms Modern JavaScript Design And Develop Copyright © 2012 by Larry Ullman
2
Objectives Properly work with HTML forms using JavaScript Create inline errors as part of form validation Create tooltips Disable the submit button upon the form’s submission
3
More Objectives Perform common JavaScript interactions with standard form elements Generate dynamically linked select menus Write regular expression patterns Test regular expressions using different methods
4
Handling Forms Recap Add submit handler to form itself Always have a submit button Always use valid action attribute HTML5 supports browser-based form validation
5
Creating Error Messages function addErrorMessage(id, msg) { 'use strict'; var elem = document.getElementById(id); // Define the new span's ID value: var newId = id + 'Error'; // Check for the existence of the span: var span = document.getElementById(newId); if (span) { span.firstChild.value = msg; // Update } else { // Create new. // Create the span: span = document.createElement('span'); span.id = newId; span.className = 'error' span.appendChild(document.createTextNode(msg)); // Add the span to the parent: elem.parentNode.appendChild(span); elem.previousSibling.className = 'error'; } // End of main IF-ELSE. } // End of addErrorMessage() function.
6
Creating Tooltips showTooltip: function(e) { 'use strict'; // Get the event object: if (typeof e == 'undefined') var e = window.event; // Get the event target: var target = e.target || e.srcElement; target.previousSibling.lastChild.style.visibility = 'visible'; }, // End of showTooltip() function.
7
Disabling the Submit Button document.getElementById('submitButton').disabled = 'disabled'; document.getElementById('submitButton').disabled = true;
8
Getting Values var data = document.getElementById('comments').value; var data = document.getElementById('selectMenu').value; var data = document.getElementById('selectMenu').selectedIndex = 3; for (var i = 0, count = elem.options.length; i < count; i++) { // Use elem.options[i].selected/value/text }
9
Linked Lists
10
Checkboxes and Radio Buttons var which = document.getElementById('someCheckbox').checked; if (which.checked) { var value = document.getElementById('someCheckbox').value; }
11
Regular Expressions A way to test strings against patterns. Complicated! Represented by RegExp objects.
12
Creating RegExp var regexp = /pattern/; var regexp = /pattern/modifier;
13
Regex Functions test() exec() search() match()
14
Metacharacters CharacterMeaning \Escape ^Indicates the beginning of the string $Indicates the end of the string.Any single character except newline |Alternatives (or) [Start of a class ]End of a class (Start of a subpattern )End of a subpattern {Start of a quantifier }End of a quantifer
15
Quantifiers CharacterMeaning ?0 or 1 *0 or more +1 or more {x}Exactly x occurrences {x,y}Between x and y (inclusive) {x,}At least x occurrences
16
Character Classes ClassShortcutMeaning [0-9]\dAny digit [\f\r\t\n\v]\sAny white space [A-Za-z0-9]\wAny word character [^0-9]\DNot a digit [^\f\r\t\n\v]\SNot white space [^A-Za-z0-9]\WNot a word character
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.