Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verifying Submitted Form Data with JavaScript Instructor:

Slides:



Advertisements
Similar presentations
17 HTML, Scripting, and Interactivity Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and.
Advertisements

WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
MWD1001 Website Production Using JavaScript with Forms.
The Web Warrior Guide to Web Design Technologies
CIS101 Introduction to Computing Week 08. Agenda Your questions JavaScript text Resume project HTML Project Six This week online Next class.
JavaScript Forms Form Validation Cookies CGI Programs.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Macromedia Dreamweaver 4 Advanced Level Course. Add Rollovers Rollovers or mouseovers are possibly the most popular effects used in designing Web pages.
Tutorial 14 Working with Forms and Regular Expressions.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (NON-audio version) © Dr. David C. Gibbs
CST JavaScript Validating Form Data with JavaScript.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
JavaScript Form Validation
Copyright 2007, Information Builders. Slide 1 Maintain & JavaScript: Two Great Tools that Work Great Together Mark Derwin and Mark Rawls Information Builders.
Department of Information Technology e-Michigan Web Development 0 HTML Form Creation in the Vignette Content Management Application.
Regular Expressions Week 07 TCNJ Web 2 Jean Chu. Regular Expressions Regular Expressions are a powerful way to validate and format text strings that may.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 9 Scott Marino.
Tutorial 14 Working with Forms and Regular Expressions.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
© Cheltenham Computer Training 2001 Macromedia Dreamweaver 4 - Slide No 1 Macromedia Dreamweaver 4 Advanced Level Course.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Dynamic Action with Macromedia Dreamweaver MX Barry Sosinsky Valda Hilley.
Using Embedded JavaScript Fort Collins, CO Copyright © XTR Systems, LLC Embedding JavaScript In HTML Instructor: Joseph DiVerdi, Ph.D., MBA.
JavaScript Part 1.
© 2000 – All Rights Reserved - Page 1 Introduction to JavaScript Programming Part Two.
MKTG 476 FRONTPAGE II Lars Perner, Instructor 1 FrontPage II File structure and references File structure and references Editing standard HTML files Editing.
Using JavaScript in FORMs Fort Collins, CO Copyright © XTR Systems, LLC Learning to Use JavaScript in HTML FORMs Instructor: Joseph DiVerdi, Ph.D., MBA.
CC1003N Week 6 More CSS and Javascript.. Objectives: ● More CSS Examples ● Javascript – introduction ● Uses of Javascript ● Variables ● Comments ● Control.
CSU - DCE Advanced Perl CGI Operation - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) on the.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 3.
Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and check boxes using HTML Add a pull-down.
CSU - DCE Webmaster I HTML - Forms - Fort Collins, CO Copyright © XTR Systems, LLC Designing Web Sites using HTML - Introduction to Forms Instructor:
1 JavaScript in Context. Server-Side Programming.
Tutorial 10 Programming with JavaScript
Tutorial 8 Programming with ActionScript 3.0. XP Objectives Review the basics of ActionScript programming Compare ActionScript 2.0 and ActionScript 3.0.
Using Client-Side Scripts to Enhance Web Applications 1.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,
1 JavaScript
Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI Programming & The CGI.pm Perl Module Instructor: Joseph DiVerdi,
The Web Wizard’s Guide To JavaScript Chapter 3 Working with Forms.
JavaScript Introduction.  JavaScript is a scripting language  A scripting language is a lightweight programming language  A JavaScript can be inserted.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Unit 10 – JavaScript Validation Instructor: Brent Presley.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Introduction to JavaScript Fort Collins, CO Copyright © XTR Systems, LLC Introduction to JavaScript Programming Instructor: Joseph DiVerdi, Ph.D., MBA.
Creating Web Documents: JavaScript Ftp / file management: review Introduction to JavaScript Sources Homework: start review for midterm, work on Project.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
CSU - DCE Introduction to CSS CSS URLs - Fort Collins, CO Copyright © XTR Systems, LLC Cascading Style Sheets - Specifying URLs Instructor: Joseph.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
JavaScript Syntax Fort Collins, CO Copyright © XTR Systems, LLC Introduction to JavaScript Syntax Instructor: Joseph DiVerdi, Ph.D., MBA.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
JavaScript, Third Edition 1 SELECTION LIST Demo. JavaScript, Third Edition 2 Description This web page will edit the slection to ensure an option was.
CSU - DCE Webmaster I HTML & URLs - Fort Collins, CO Copyright © XTR Systems, LLC Designing Web Sites With HTML - Using Effective Links Instructor:
Chapter 5 Validating Form Data with JavaScript
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Section 17.1 Section 17.2 Add an audio file using HTML
Web Programming– UFCFB Lecture 17
WEB PROGRAMMING JavaScript.
Presentation transcript:

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verifying Submitted Form Data with JavaScript Instructor: Joseph DiVerdi, Ph.D., MBA

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Introduction One of the Most Important Uses of JavaScript –Performed Using HTML Event Handlers –Together With JavaScript Functions Verification of Form Data Prior to Submission –Important Because Preserves Internet Bandwidth Saves Viewer Time Good Excuse to Learn JavaScript

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Ice Cream Survey Example Shortcut to Creating a Copy on Your Site –Use Any Other Procedure If You Prefer Examine Basic Survey Form on Course Site –Click on Link to View Page Take a Look at the Page –View Page Source, Select All, & Copy –Open New File on Server with HTML-Kit Named survey.html –Paste into New File & Save

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Form Verification Rules Ice Cream Flavor Rule –An Ice Cream Flavor Radio Box Must Be Checked

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Form Verification Rules Toppings Rule –Any Number of Toppings Can Be Checked Zero or More

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Form Verification Rules Name & Address Submission Rules –Neither a Name nor Address is Required –If Either a Name or Address is Supplied So Must the Other

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Form Verification Rules Name Rules –A Name Must Consist of Only Alphameric Characters & Certain Others a-z A-Z dash quote space –A Name Must Not Consist Solely of Spaces

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Form Verification Rules Address Rules –A Supplied Address Must Be Valid For Example –Name Must Consist Only of a-z A-Z 0-9 period dash underscore –There Must Be One or More mid_domains –mid_domain Must Consist Only of a-z A-Z 0-9 dash underscore –top_domain Must Be Two or Three Alphameric Characters

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Form Modifications Set Form ACTION, NAME, METHOD Create External JavaScript Directory & File Add SCRIPT Container For External File Add ONSUBMIT Event Handler to the Form Convert Form Variables to JavaScript Compatible Names Create JavaScript Functions in File

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Edit FORM Tag Set Form ACTION, NAME, METHOD Create External JavaScript Directory & File Add SCRIPT Container For External File Add ONSUBMIT Event Handler to the Form Convert Form Variables to JavaScript Compatible Names Create JavaScript Functions in File

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Edit FORM Tag <FORM ACTION=" METHOD="POST" NAME="ice_cream_survey" > ACTION Identifies CGI Program METHOD Specifies Data Transfer Method NAME Provides FORM With Identity Test Survey Now

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Create Directory & File Set Form ACTION, NAME, METHOD Create External JavaScript Directory & File Add SCRIPT Container For External File Add ONSUBMIT Event Handler to the Form Convert Form Variables to JavaScript Compatible Names Create JavaScript Functions in File

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Use External JavaScript

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Contents of form_verify.js // top level function verifying that submitted form data meets rules function verify(form) { // if we made it down to this point then everything is OK return true; }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add External Reference Set Form ACTION, NAME, METHOD Create External JavaScript Directory & File Add SCRIPT Container For External File Add ONSUBMIT Event Handler to the Form Convert Form Variables to JavaScript Compatible Names Create JavaScript Functions in File

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add External Reference

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add Event Handler Set Form ACTION, NAME, METHOD Create External JavaScript Directory & File Add SCRIPT Container For External File Add ONSUBMIT Event Handler to the Form Convert Form Variables to JavaScript Compatible Names Create JavaScript Functions in File

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add Event Handler <FORM ACTION=" METHOD="POST" NAME="ice_cream_survey" ONSUBMIT="return verify(ice_cream_survey)" > ONSUBMIT Causes JavaScript Function to Be Executed When Viewer Hits Submit Button Test Survey Now

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Fix Form Variables Set Form ACTION, NAME, METHOD Create External JavaScript Directory & File Add SCRIPT Container For External File Add ONSUBMIT Event Handler to the Form Convert Form Variables to JavaScript Compatible Names Create JavaScript Functions in File

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Fix Form Variables JavaScript Variable Name Rules –Composed of Any Number of Alphanumerics Plus The Underscore & Dollar Sign a-z A-Z 0-9 _ $ –The First Character Must Not Be a Digit Change the FORM Variable Names ice cream flavor -> ice_cream_flavor address -> _address Adopt This Convention in Your Future Forms

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Form Modifications Set Form ACTION, NAME, METHOD Create External JavaScript Directory & File Add SCRIPT Container For External File Add ONSUBMIT Event Handler to the Form Convert Form Variables to JavaScript Compatible Names Create JavaScript Functions in File

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verification Logic Flow Perform a Series of Tests in Verify Function If All Tests Are Passed –Verify Function Will Return True FORM ACTION Will be Performed If any Test Fails –Verify Function Will Return false FORM ACTION Will Not be Performed –Verify Function Also Brings Up alert window –Verify Function Also Positions Insert Point On Offending Input Element

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verification Logic Flow Test If an Ice Cream Flavor is Checked –If Checked Proceed to Next Test –If Not Checked Display Alert Return False

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verification Logic Flow Test Length of Name & Address –If Both Equal Zero Return True Perform ACTION –If Either or Both are Not Equal to Zero Proceed to Next Test

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verification Logic Flow Test Name & Address Lengths –If Name Length Not Equal to Zero& Length Equal to Zero Display Alert Focus on Name Return False

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verification Logic Flow Test Name & Address Lengths –If Name Length Equal to Zero& Length Not Equal to Zero Display Alert Focus on Address Return False

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verification Logic Flow Proceed to Next Test –If We Arrive at This Point Then Name & Address Lengths Are Both Non-Zero

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verification Logic Flow Test if Name is Valid –If Only Contains Whitespace Display Alert Focus on Name Return False –Otherwise Proceed to Next Test

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verification Logic Flow Test if Address is Valid –If Doesn't Contain Well-Formatted Address Display Alert Focus on Address Return False –Otherwise All Tests Passed Return True Perform ACTION

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC JavaScript Conditionals Use if Statements to Enforce Rules if (this_statement_is_true)) { execute_the_statements_found_here; } // ensure that a radio button has been checked if (form_object_is_not_checked(form.ice_cream_flavor)) { window.alert("You've got to select an ice cream flavor."); return false; }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add Checked Test Code // top level function verifying that submitted form data meets rules function verify(form) { // ensure that a radio button has been checked if (form_object_is_not_checked(form.ice_cream_flavor)) { window.alert("You've got to select an ice cream flavor."); return false; } // if we made it down to this point then everything is OK return true; }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add Function // if we made it down to this point then everything is OK return true; } // function to test if a radio group element has been checked function form_object_is_not_checked(object) { for (var item = 0; item < object.length; item++) { if (object[item].checked) { return false; } } return true; }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add More Test Code // ensure that a radio button has been checked if (form_object_is_not_checked(form.ice_cream_flavor)) { window.alert("You've got to select an ice cream flavor."); return false; } // 1st case: neither a name or address has been entered - everything is OK if (form.name.value.length == 0 && form. _address.value.length == 0) { return true; } // if we made it down to this point then everything is OK return true; }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add More Test Code // 1st case: neither a name nor address is entered - everything is OK if (form.name.value.length == 0 && form. _address.value.length == 0) { return true; } // 2nd case: only a name has been entered - oops if (form.name.value.length != 0 && form. _address.value.length == 0) { window.alert("If you're going to supply a name, you must to supply an address."); form. _address.focus(); return false; } // if we made it down to this point then everything is OK return true; }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add More Test Code return false; } // 3rd case: only an address has been entered - oops if (form.name.value.length == 0 && form. _address.value.length != 0) { window.alert("If you're going to supply an address, you must to supply a name."); form.name.focus(); return false; } // if we made it down to this point then everything is OK return true; }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add More Test Code // 3rd case: only an address has been entered - oops if (form.name.value.length == 0 && form. _address.value.length != 0) { window.alert("If you're going to supply an address, you must to supply a name."); form.name.focus(); return false; } // 4th case: both a name and address have been entered // n.b. there's no need to test for this case because the only way we can find ourselves here // is if the other three cases failed. Right? // if we made it down to this point then everything is OK return true; }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add More Test Code // 4th case: both a name and address have been entered // n.b. there's no need to test for this case because the only way we can find ourselves here // is if the other three cases failed. Right? // check for a "valid" name if (!name_is_valid(form.name)) { window.alert("If you're going to supply a name, it has to be a valid one."); form.name.focus(); return false; } // if we made it down to this point then everything is OK return true; }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add More Test Code... if (object[item].checked) { return false; } } return true; } // regular expression to test for proper formatting of a first name // n.b. two tests must be passed for this function to return true // 1. name must not be made solely of blanks // 2. name must contain only a-z, A-Z, ', -, and space function name_is_valid(object) { if (object.value.match(/^\s+$/)) { return false; } return !object.value.match(/[^a-zA-Z\- ']/); }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add More Test Code if (!name_is_valid(form.name)) { window.alert("If you're going to supply a name, it has to be a valid one."); form.name.focus(); return false; } // check for a "valid" address if (! _address_is_valid(form. _address)) { window.alert("If you're going to supply an address, it has to be a well formatted one."); form. _address.focus(); return false; } // if we made it down to this point then everything is OK return true; }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Add More Test Code // regular expression to test for proper formatting of a first name // n.b. two tests must be passed for this function to return true // 1. name must not be made solely of blanks // 2. name must contain no characters other than a-z, A-Z, ', -, and space function name_is_valid(object) { if (object.value.match(/^\s+$/)) { return false; } return !object.value.match(/[^a-zA-Z\- ']/); } // regular expression to test for proper formatting of address function _address_is_valid(object) { return object.value.match( }

Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Congratulations You have successfully created a complete JavaScript program You have learned –How to integrate JavaScript & HTML –How to access several types of HTML objects –How to write JavaScript functions –How to use HTML event handlers