Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Similar documents
The Critical-Path Algorithm

Minimal Spanning Trees

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

PHP Queries and HTML Forms Lecture 23

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Sampling Distribution Examples Sections 15.4, 15.5

The Decreasing-Time Algorithm

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Rotations and Translations

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Building the Abstract Syntax Trees

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Javascript Lecture 23

Recursive Descent Parsers

XML and AJAX Lecture 28

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Scheduling and Digraphs

The Class Construct Part 1

Density Curves Sections

The Pairwise-Comparison Method

Introduction to Databases

The Traveling Salesman Problem Brute Force Method

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Function Definition Syntax Tree

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Solving Recursive Sequences by Iteration

LR Parsing - Conflicts

Scope and Parameter Passing

The Graphics Pipeline

Inheritance: The Fundamental Functions

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

DTDs and XML Attributes

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Magnification and Minification

Scope and Parameter Passing

Street-Routing Problems

Implementing Linked Lists

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Friends and Unary Operators

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Coefficient of Determination

Insertions, Deletions, and Updates

Recursive Linked Lists

Displaying Distributions - Quantitative Variables

Basic PHP Lecture 17

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The Plurality-with-Elimination Method

Nondeterministic Programming in C++

Binary Tree Applications

Introduction to Databases

XQuery FLOWR Expressions Lecture 35

The Graphics Pipeline

Recognition of Tokens

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

The CYK Parsing Algorithm

The Normal Distribution

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Inheritance: The Fundamental Functions

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

The Traveling Salesman Problem Cheapest-Link Algorithm

Introduction to Compiler Design

XML. Lecture 29. Robb T. Koether. Fri, Mar 28, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XML Fri, Mar 28, / 23

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Programming Languages

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

Programming Languages

Basic HTML. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 20, 2013

The x86 Instruction Set

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

The Projection Matrix

Stacks and their Applications

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Basic CSS Lecture 17

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

List Iterator Implementation

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Ambient and Diffuse Light

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

Basic HTML. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 20, 2013

PHP Arrays. Lecture 20. Robb T. Koether. Hampden-Sydney College. Wed, Feb 28, 2018

Relational Databases

The Class Construct Part 2

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

Relational Databases Lecture 2

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Transcription:

Form Validation Lecture 25 Robb T. Koether Hampden-Sydney College Wed, Mar 23, 2018 Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 1 / 16

1 Form Validation 2 Detecting Javascript Errors 3 The $() Function 4 Assignment Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 2 / 16

Outline 1 Form Validation 2 Detecting Javascript Errors 3 The $() Function 4 Assignment Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 3 / 16

Form Validation An HTML form should attempt to validate the information that is entered before sending that information to the PHP file. At a minimum, it should verify that every required field is filled. It can also check that numerical values are within a reasonable range, etc. Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 4 / 16

Form Validation Form Validation <form method="post" onsubmit="return validate()" action="profile.php"> : </form> The onsubmit event handler will invoke Javascript when the submit button is pressed. Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 5 / 16

Form Validation The validate() Function function validate() var fname = document.getelementbyid("fname").value; if (fname == "") alert("please enter your first name"); return false; return true; This function checks that the fname element is not blank. Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 6 / 16

Form Validation The validate() Function function validate() var age = document.getelementbyid("age").value; age = parseint(age); if (age <= 0 age > 120) alert("please enter a valid age"); return false; return true; This function checks that age is a reasonable number. Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 7 / 16

Form Validation The validate() Function function validate() var state = document.getelementbyid("state").value; if (state == "0") alert("please select your state"); return false; return true; To detect errors from a pull-down menu, set a default value of 0. Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 8 / 16

Outline 1 Form Validation 2 Detecting Javascript Errors 3 The $() Function 4 Assignment Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 9 / 16

The onerror Event Handler The onerror Event Handler onerror = errorhandler; The global onerror event handler can be used to direct execution to a specified function if a Javascript error occurs. Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 10 / 16

The errorhandler Function The errorhandler Function function errorhandler(message, url, line) errmsg = "Sorry, an error was encountered\n\n"; errmsg += "Error: " + message + "\n"; errmsg += "URL: " + url + "\n"; errmsg += "Line: " + line + "\n"; errmsg += "Click OK to continue.\n\n"; alert(errmsg); return true; The values of message, url, and line are automatically generated. In this example, the errorhandler() function will be called if a Javascript error occurs. Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 11 / 16

Outline 1 Form Validation 2 Detecting Javascript Errors 3 The $() Function 4 Assignment Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 12 / 16

The $() Function The getelementbyid() Function function $(id) return document.getelementbyid(id); function validate() var fname = $("fname").value; : The getelemmentbyid() function is used so frequently that programmers often write a function called $() to replace it. Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 13 / 16

The $() Function The validate() Function function validate() var fname = $("fname").value; if (fname == "") alert("please enter your first name"); return false; return true; The earlier example, now using $(). Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 14 / 16

Outline 1 Form Validation 2 Detecting Javascript Errors 3 The $() Function 4 Assignment Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 15 / 16

Assignment Assignment Visit the W3Schools website http://www.w3schools.com/js/js_form_validation.asp Visit the section labeled JS Validation. Visit the EchoEcho website http://www.echoecho.com/jsforms.htm Visit that page and subsequent pages. Incorporate validity-checking into your Tigerbook application wherever applicable. Robb T. Koether (Hampden-Sydney College) Form Validation Wed, Mar 23, 2018 16 / 16