UMass Lowell Dept. of Computer Science
91.461 GUI Programming I
Fall 2015 Semester, Section 201
Prof. Jesse M. Heines
Notes for Class No. 15
Introduction to jQuery (continued)
Thursday, October 22, 2015
A video of this class is (or will be) posted at: http://echo360.uml.edu/heines201516/guiprogramming1.html
Handouts and Materials
Assignment No. 7 is now posted
- this assignment requires material covered in Chapter 9 of the JavaScript and jQuery text
Openings / Announcements / Reminders
Quiz on Chapters 1-4 of the JavaScript & jQuery text today
Faculty Advising Period begins next week — Monday, October 26
- many of you haven’t looked at the Grade Display Program in quite a while
- remember that you must achieve a C or better in this course to get into 91.462 GUI Programming II
- if your average at this point is below 80, you are in danger of not getting into 91.462
Class Notes
Related reading for this class: JS&jQ:TMM Ch. 4, 5 & 9
Form Events (JS&jQ:TMM p. 263-268)
submit
— triggered when the form is submitted
- useful for validation, because this even occurs before the form’s action is taken
focus
— triggered when a control gains focus
- can also be used to set focus to a control
blur
— triggered when a control loses focus
click
— triggered when a control is clicked
change
— triggered whenever a control is changed
- note that these events are not triggered if a control is disabled
Various Versions of the Multiplication Table Generator Program
Basic Validation using JavaScript and jQuery
Using the jQuery submit()
event handler
1 $(document).ready( function() {
2 tblInitializer.initialize() ;
3 tblGenerator.populateMultiplicationTable_jQuery( "#placeholder", true ) ;
4 $('#frm').submit( function() { // trap the submit event *before* the action is executed
5 return validateForm() ; // true = allow action to proceed, false = inhibit action
6 } ) ;
7 } ) ; // end ready
Writing validation tests
- retrieving and testing form field data
- the jQuery
val()
function
- the jQuery
html()
function
- the JavaScript
isNaN()
function
- retrieving the contents of non-textual controls
- using the
return
statement efficiently
- ordering your tests efficiently
- writing helper functions
- displaying error message on the screen
- consistent location
- other visual clues
- setting focus to the erroneous field
Three pillars of good error messages:
- identify where the error occurred — direct the user to the precise location of the error
- identify why the error occurred — explain exactly what the problem is
- suggest how to correct the error — provide information to guide the user to correcting his/her entry
Initializing form fields
- issue: when the form is reloaded, the form’s text fields will be cleared
- “visual integrity”
- if the user wants to regenerate the table, he or she probably won’t want to change them all
- solution: put the values read from the passed parameters back into the form’s text fields as defaults
- suggestion: use this as a way to test that you are reading the passed parameters properly
- best practice: don’t assign the passed values to the variables that control the table’s display until you know they’re valid
- what constitutes valid?
- first, and most obviously, that they are numbers
- use the JavaScript
isNaN()
function?
- however, even before that you should check whether the parameter exists
- you might also check that
xBegin <= xEnd
and yBegin <= yEnd
Form Validation Demonstration
developed with student Alan Szmyt, Fall 2015 semester
As posted on Piazza, the problem with the code developed in class on Tuesday was simply that the JavaScript tried to populate the “message” paragraph before the page had finished loading.
- At that point, that paragraph did not yet exist, and unfortunately JavaScript doesn’t print an error message in this instance.
- The fix was to enclose the
$("#message").html( ... )
line in the $(document).ready()
function as shown in the code below.
Note addition to line 60 from what’s on the handout:
60 if ( parameters !== null && parameters !== "" ) {
Live Demo