eg-259:lecture9
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
eg-259:lecture9 [2013/02/20 14:46] – [Handling Events from Textbox and Password Elements] eechris | eg-259:lecture9 [2013/02/26 08:33] (current) – [Previous Example] eechris | ||
---|---|---|---|
Line 58: | Line 58: | ||
* What purpose does the // | * What purpose does the // | ||
+ | ===== Previous Example ===== | ||
+ | Used HTML event attributes | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
===== Handling Events from Button Elements (more) ===== | ===== Handling Events from Button Elements (more) ===== | ||
Line 405: | Line 409: | ||
---- | ---- | ||
- | * Code: | + | * Code HTML: |
- | <source | + | <code html> |
+ | < | ||
+ | <!-- pswd_chk.html | ||
+ | An example of input password checking, using the onsubmit event | ||
+ | --> | ||
+ | <html class=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | < | ||
+ | <meta name=" | ||
+ | |||
+ | <link rel=" | ||
+ | <link rel=" | ||
+ | <link rel=" | ||
+ | |||
+ | <script src=" | ||
+ | </ | ||
+ | < | ||
+ | <div class=" | ||
+ | <h1> Illustrate password checking </ | ||
+ | <h2> Choose a Password </ | ||
+ | <form id = " | ||
+ | <p> | ||
+ | <label for=" | ||
+ | <input type=" | ||
+ | <label for=" | ||
+ | <input type=" | ||
+ | </ | ||
+ | <p> | ||
+ | <input class=" | ||
+ | <input class=" | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | <script src="// | ||
+ | < | ||
+ | |||
+ | <script src=" | ||
+ | <script src=" | ||
+ | <!-- Any external JS loaded here --> | ||
+ | <script src="pswd_chk.js"></ | ||
+ | </ | ||
+ | </html> | ||
+ | </ | ||
+ | * Code JavaScript: | ||
+ | < | ||
+ | // The event handler for password checking | ||
+ | |||
+ | var chkPasswords = function () { | ||
+ | var password = document.getElementById(" | ||
+ | var confirmation = document.getElementById(" | ||
+ | if (password.value === "" | ||
+ | alert(" | ||
+ | password.focus(); | ||
+ | return false; | ||
+ | } | ||
+ | if (password.value !== confirmation.value) { | ||
+ | alert(" | ||
+ | password.focus(); | ||
+ | password.select(); | ||
+ | return false; | ||
+ | } else { | ||
+ | return true; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | // Set submit button onsubmit property to the event handler | ||
+ | |||
+ | document.getElementById(" | ||
+ | document.getElementById(" | ||
+ | </code> | ||
* Result: | * Result: | ||
{{eg-259: | {{eg-259: | ||
- | |||
Line 421: | Line 497: | ||
* The event handler will be triggered by the change event of the text boxes for | * The event handler will be triggered by the change event of the text boxes for | ||
* If an error is found in either, an alert message is produced and both focus and select are called on the text box element | * If an error is found in either, an alert message is produced and both focus and select are called on the text box element | ||
- | * Here is the result: [[/ | + | * Here is the result: [[http:// |
---- | ---- | ||
- | * Code: | + | * Code HTML: |
- | <source | + | <code html> |
+ | < | ||
+ | <!-- validator.html | ||
+ | An example of input validation using the change and submit | ||
+ | events | ||
+ | --> | ||
+ | <html class=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | < | ||
+ | <meta name=" | ||
+ | |||
+ | <link rel=" | ||
+ | <link rel=" | ||
+ | <link rel=" | ||
+ | |||
+ | <script src=" | ||
+ | </head> | ||
+ | < | ||
+ | <div class=" | ||
+ | < | ||
+ | < | ||
+ | <form method=" | ||
+ | <Label for=" | ||
+ | <div class=" | ||
+ | <input class=" | ||
+ | | ||
+ | <span class=" | ||
+ | </ | ||
+ | <div class=" | ||
+ | <label for=" | ||
+ | <input class=" | ||
+ | | ||
+ | <span class=" | ||
+ | </ | ||
+ | <p> | ||
+ | <input class=" | ||
+ | <input class=" | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | <script src="// | ||
+ | < | ||
+ | |||
+ | <script src=" | ||
+ | <script src=" | ||
+ | <!-- Any external JS loaded here --> | ||
+ | <script src="validator.js"></ | ||
+ | </ | ||
+ | </html> | ||
+ | </ | ||
+ | * Code JavsScript: | ||
+ | <code javascript> | ||
+ | // The event handler function for the name text box | ||
+ | |||
+ | var chkName = function() { | ||
+ | var myName = document.getElementById(" | ||
+ | |||
+ | // Test the format of the input name | ||
+ | // Allow the spaces after the commas to be optional | ||
+ | // Allow the period after the initial to be optional | ||
+ | |||
+ | var pos = myName.value.search(/ | ||
+ | |||
+ | if (pos != 0) { | ||
+ | alert(" | ||
+ | "The correct form is: " + "Last name, First name, Middle initial \n" + | ||
+ | " | ||
+ | myName.focus(); | ||
+ | myName.select(); | ||
+ | return false; | ||
+ | } else | ||
+ | return true; | ||
+ | }; | ||
+ | |||
+ | // The event handler function for the phone number text box | ||
+ | |||
+ | var chkPhone = function() { | ||
+ | var myPhone = document.getElementById(" | ||
+ | |||
+ | // Test the format of the input phone number | ||
+ | |||
+ | var pos = myPhone.value.search(/ | ||
+ | |||
+ | if (pos != 0) { | ||
+ | alert(" | ||
+ | "The correct form is: ddd-ddd-dddd \n" + | ||
+ | " | ||
+ | myPhone.focus(); | ||
+ | myPhone.select(); | ||
+ | return false; | ||
+ | } else | ||
+ | return true; | ||
+ | }; | ||
+ | |||
+ | var validate_form = function() { | ||
+ | return (chkName() && chkPhone()); | ||
+ | }; | ||
+ | |||
+ | |||
+ | // Set form element object properties to their | ||
+ | // corresponding event handler functions | ||
+ | |||
+ | document.getElementById(" | ||
+ | document.getElementById(" | ||
+ | document.getElementById(" | ||
+ | </code> | ||
* Result: | * Result: | ||
{{eg-259: | {{eg-259: | ||
- | |||
===== The DOM 2 Event Model ===== | ===== The DOM 2 Event Model ===== | ||
Line 497: | Line 681: | ||
===== DOM 2 Example ===== | ===== DOM 2 Example ===== | ||
- | * A revision of // | + | * A revision of // |
+ | * Note that this doesn' | ||
+ | * A solution for this is left as an exercise. | ||
---- | ---- | ||
- | * Code: | + | * Code HTML is almost the same as for previous example. Only JavaScript definition of the event registration and the event handlers changes when updating to DOM 2 events. |
- | <source http://eng-hope.swan.ac.uk/eg-259/examples/lecture9/validator2.html javascript|validator2.html> | + | <code javascript> |
+ | // validator2.js | ||
+ | // | ||
+ | // | ||
+ | |||
+ | // | ||
+ | |||
+ | var chkName = function(event) { | ||
+ | |||
+ | | ||
+ | var myName = event.currentTarget; | ||
+ | |||
+ | // Test the format of the input name | ||
+ | // Allow the spaces after the commas to be optional | ||
+ | // Allow the period after the initial to be optional | ||
+ | |||
+ | var pos = myName.value.search(/ | ||
+ | |||
+ | if (pos != 0) { | ||
+ | alert(" | ||
+ | "The correct form is: " + "Last name, First name, Middle initial \n" + | ||
+ | " | ||
+ | myName.focus(); | ||
+ | myName.select(); | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | // The event handler function for the phone number text box | ||
+ | |||
+ | var chkPhone = function(event) { | ||
+ | |||
+ | var myPhone = event.currentTarget; | ||
+ | |||
+ | // Test the format of the input phone number | ||
+ | |||
+ | var pos = myPhone.value.search(/ | ||
+ | |||
+ | if (pos != 0) { | ||
+ | alert(" | ||
+ | "The correct form is: ddd-ddd-dddd \n" + | ||
+ | " | ||
+ | myPhone.focus(); | ||
+ | myPhone.select(); | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | |||
+ | // Set form element object properties to their | ||
+ | // corresponding event handler functions | ||
+ | |||
+ | // Get the DOM addresses of the elements and register | ||
+ | // the event handlers | ||
+ | |||
+ | var customerNode = document.getElementById(" | ||
+ | var phoneNode = document.getElementById(" | ||
+ | |||
+ | customerNode.addEventListener(" | ||
+ | phoneNode.addEventListener(" | ||
+ | |||
+ | // Challenge ... how would you check both name and telephone number on submit? | ||
+ | </code> | ||
< | < | ||
DOM 0 and DOM 2 event handling can be mixed in a document | DOM 0 and DOM 2 event handling can be mixed in a document | ||
</ | </ | ||
- | |||
Line 522: | Line 767: | ||
* The '' | * The '' | ||
* The '' | * The '' | ||
- | * Example: [[/ | + | * Example: [[http:// |
---- | ---- | ||
Line 533: | Line 778: | ||
* Code: | * Code: | ||
- | <source http://eng-hope.swan.ac.uk/eg-259/examples/lecture9/navigate.html javascript|navigate.html> | + | <code html> |
+ | < | ||
+ | <!-- navigate.html | ||
+ | An example of using the navigator object | ||
+ | --> | ||
+ | <html lang=" | ||
+ | < | ||
+ | <meta charset=" | ||
+ | < | ||
+ | < | ||
+ | // The event handler function to display the browser name | ||
+ | // and its version number | ||
+ | |||
+ | function navProperties() { | ||
+ | alert(" | ||
+ | "The version number is: " + navigator.appVersion + " | ||
+ | } | ||
+ | </script> | ||
+ | </head> | ||
+ | <body onload = " | ||
+ | </html> | ||
+ | </code> | ||
===== Summary of This Lecture ===== | ===== Summary of This Lecture ===== |
eg-259/lecture9.1361371585.txt.gz · Last modified: 2013/02/20 14:46 by eechris