UMass Lowell Dept. of Computer Science

91.461 GUI Programming I

Fall 2015 Semester, Section 201

Prof. Jesse M. Heines

Notes for Class No. 13

Processing Forms with JavaScript, including Basic Form Validation Concepts and Introduction to jQuery

Thursday, October 15, 2015

A video of this class is (or will be) posted at:   http://echo360.uml.edu/heines201516/guiprogramming1.html


Handouts and Materials


Openings / Announcements / Reminders

Please complete the Midterm Course Evaluation

https://docs.google.com/forms/d/1dHTYaWtph67108xNBn2XA5ezyr3XdrqoGjWep86Nxgk/viewform

Aso of Wednesday, October 14, at 11:00 AM, only 16 students had completed the form
As of this morning, Thursday, October 15, at 8:45 AM, 18 students had completed the form

Free Form Questions

This course would be better if ...

I would learn more from this course if ...

Prof. Heines would be a more effective teacher if ...

Please write any additional comments (positive or negative) and/or suggestions you have for improving this course. These may relate to any aspect of the course at all or anything that I could do to help you learn.


Assignment No. 5 Issues


Upcoming Quiz on Chapters 1-4 of the JavaScript & jQuery text


Class Notes

Related reading for this class:  JS&jQ:TMM Ch. 4, 5 & 9


A Whirlwind Look at JavaScript  (continued)

Key concept:  FUNCTIONS ARE SIMPLY EXECUTABLE VARIABLES

Run the above function


Functions as First-Class Objects  (Stefanov, Ch. 4)


Using Forms with Assignment No. 6

Creating Forms  (partial review from last class)

Using Buttons


Processing Form Data with JavaScript

Working with the location object

http://jesseheines.com/~heines/91.461/91.461-2015-16f/461-lecs/lecture12.jsp?one=1&two=2&three=3

  1. location.search
  2. location.search.substr(1)
  3. location.search.substr(1).split("&")
  4. location.search.substr(1).split("&").length
  5. location.search.substr(1).split("&")[0].split("=")
  6. location.search.substr(1).split("&")[1].split("=")
  7. location.search.substr(1).split("&")[0].split("=")[0]
  8. location.search.substr(1).split("&")[0].split("=")[1]
  9. obj = {} ;
  10. obj
  11. obj[location.search.substr(1).split("&")[0].split("=")[0]] = location.search.substr(1).split("&")[0].split("=")[1]
  12. obj
    • Object{ one = "1" }

Demonstration

Input

http://jesseheines.com/~heines/91.461/91.461-2015-16f/461-lecs/lecture13.jsp

Output

Live Demo


Global Methods (Functions)Deitel Fig. 11.13 on p. 370 (1st ed.), Fig. 10.8 on p. 341 (2nd ed.), Fig. 9.9 on p. 345 (4th ed.)

Reference: Deitel, P.J. & Deitel, H.M.  Internet & World Wide Web How To Program, 4th edition.  pp. 345-346.

Global
Function
Description
escape Takes a string argument and returns a string in which all spaces, punctuation, accent characters and any other character that is not in the ASCII character set are encoded in a hexadecimal format that can be represented on all platforms.
unescape Takes a string as its argument and returns a string in which all characters previously encoded with escape are decoded.
eval Takes a string argument representing JavaScript code to execute.  The JavaScript interpreter evaluates the code and executes it when the eval function is called.  This function allows Java­Script code to be stored as strings and executed dynamically.  Note: It is considered a serious security risk to use eval to process any data entered by a user because a malicious user could exploit this to run dangerous code.
isFinite Takes a numeric argument and returns true if the value of the argument is not NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY (values that are not numbers or numbers outside the range that JavaScript supports)—otherwise, the function returns false.
isNaN Takes a numeric argument and returns true if the value of the argument is not a number; otherwise, it returns false.  The function is commonly used with the return value of parseInt or parseFloat to determine whether the result is a proper numeric value.
parseFloat Takes a string argument and attempts to convert the beginning of the string into a floating-point value.  If the conversion is unsuccessful, the function returns NaN; otherwise, it returns the converted value (e.g., parseFloat("abc123.45") returns NaN, and parseFloat("123.45abc") returns the value 123.45).
parseInt Takes a string argument and attempts to convert the beginning of the string into an integer value.  If the conversion is unsuccessful, the function returns NaN; otherwise, it returns the converted value (e.g., parseInt("abc123") returns NaN, and parseInt("123abc") returns the integer value 123).  This function takes an optional second argument, from 2 to 36, specifying the radix (or base) of the number.  Base 2 indicates that the first argument string is in binary format, base 8 indicates that the first argument string is in octal format and base 16 indicates that the first argument string is in hexadecimal format.


The Math Package


Scaffolding, Decorating, and Activating

Scaffolding

HTML5 right CSS3 right JavaScript right jQuery

Photo Credits

As you surely realize by now, JavaScript is no longer your father’s (or your grandfather’s) toy language for making little things happen change on web pages

The key to using JavaScript, however, is not the language itself


Anatomy of a jQuery Sstatement

jQNN p. 12
[figure source:  jQuery: Novice to Ninja, by Castledine & Sharkie, p. 12]


Examples

The following examples and page references in orange are from jQuery: Novice to Ninja, by Castledine & Sharkie

Examples in jQuery: Novice to Ninja and JS&jQ:TMM Ch. 4

Adding and removing classes (pp. 30-31)


Form Events  (JS&jQ:TMM p. 263-268)


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

Remember the three pillars of good error messages:

Initializing form fields



This is document http://jesseheines.com:8080/~heines/91.461/91.461-2015-16f/461-lecs/lecture13.jsp.  It was last modified on Friday, August 26, 2022 at 4:09 PM.
Copyright © 2022 by Jesse M. Heines.  All rights reserved.  May be freely copied or excerpted for educational purposes with credit to the author.