Unit 10 – JavaScript Validation Instructor: Brent Presley.

Slides:



Advertisements
Similar presentations
Tutorial 6 Creating a Web Form
Advertisements

Ch3: Introduction to HTML5 part 2 Dr. Abdullah Almutairi ISC 340 Fall 2014.
The Web Warrior Guide to Web Design Technologies
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
SE-2840 Dr. Mark L. Hornick 1 HTML input elements and forms.
MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.
Computer Science 103 Chapter 4 Advanced JavaScript.
Tutorial 14 Working with Forms and Regular Expressions.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Lesson 8 Creating Forms with JavaScript
CST JavaScript Validating Form Data with JavaScript.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
Internet & World Wide Web How to Program, 5/e Copyright © Pearson, Inc All Rights Reserved.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
JavaScript Form Validation
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
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.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
Tutorial 14 Working with Forms and Regular Expressions.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Neal Stublen Improvements  New form elements  Assist with validation  Consistency across sites  Changes reduce the need for common.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 3.
Multi-Part Requests/ Parent & Child Service Items.
Lesson13. JavaScript JavaScript is an interpreted language, designed to function within a web browser. It can also be used on the server.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
1 Forms and Form elements CSD 340 McCoey. 2 Form Object Properties action Usually a call to a server elements encoding method post or get target Methods.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
1 HTML Forms
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
HTML Forms. Slide 2 Forms (Introduction) The purpose of input forms Organizing forms with a and Using different element types to get user input A brief.
JavaScript, Fourth Edition
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
HTML Forms.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
1 HTML forms (cont.)
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Beginning ASP.NET in C# and VB Chapter 9
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
JavaScript JavaScript is a programming language that web browsers understand. You can use it to make your web pages interactive by: Responding to user.
Tutorial 6 Creating a Web Form
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript, Third Edition 1 SELECTION LIST Demo. JavaScript, Third Edition 2 Description This web page will edit the slection to ensure an option was.
JavaScript, Sixth Edition
Build in Objects In JavaScript, almost "everything" is an object.
In this session, you will learn to:
Data Validation and Protecting Workbook
Web Programming– UFCFB Lecture 17
IS333: MULTI-TIER APPLICATION DEVELOPMENT
Conditions and Ifs BIS1523 – Lecture 8.
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript and Ajax (JavaScript Events)
Murach's JavaScript and jQuery (3rd Ed.)
Presentation transcript:

Unit 10 – JavaScript Validation Instructor: Brent Presley

LINK BUTTONS TO JS FUNCTION Ways to link buttons to JS functions (submit implied) Using is also an option, but if the user’s browser JavaScript is turned off, no submit action will ever occur. When creating web forms you should always consider the user whose JavaScript is turned off

LINKING FORM BUTTON TO JS add onclick event to button add onsubmit to form tag

LINKING FORM BUTTON TO JS add to window.onload event handler –onsubmit or onclick

OTHER ITEMS RE: LINKING Note the quotes (single or double) around function name. Note the ( ) after function name if on the button, otherwise no () –at one point we are calling the function, where in the onload event handler, we are not calling the function at that time

INTERCEPTING THE NORMAL PROCESSING OF A SUBMIT/RESET BUTTON Normally, when you click a button designated as submit or image, the form action is processed. If action is the empty string, the form is cleared You can intercept the normal processing, do some JavaScript processing (validation?) and then continue with the normal processing or cancel the normal processing. This can also be done for the reset button.

USING RETURN IN ONCLICK EVENT In the button tag, add the keyword return before the function name if you wish to potentially cancel the submit. In the function, determine if the form should be submitted If the form should be submitted, return true from the function or don’t include a return statement If the form should NOT be submitted, return false.

ADDITIONAL ONCLICK INFO Note the form’s button designates an onclick event (calling buttonClick) preceded by the keyword return The button could also be defined using the input tag (type submit or image) The JavaScript function, buttonClick (pick a better name) does whatever processing is needed. At some point, it checks to see if everything is alright and if so, it returns true. The submit command probably posts the data to some other form, effectively closing this form. The returned value of true, tells the form to go ahead with the submit process When the if statement is false, the function returns false, which tells the form to cancel the submit it would have normally done.

ONSUBMIT EVENT ON FORM This also works if you add an onsubmit event to the form. This technique would execute buttonClick whenever any submit button (type=submit or image) is clicked. You wouldn’t use this and a button onclick at the same time.

CREATING EVENT HANDLERS Personally, I don’t like adding onclick/onsubmit attributes to HTML tags. Blends HTML and JavaScript, making the JavaScript hard to find. Violates separation of logic I prefer to link all event handlers in the window.onload event You should already have this event in your JavaScript if you included JavaScript to correctly clear a Firefox form

CALLING FUNCTIONS IN ONLOAD The critical point here is to recognize that none of the function names contain (parenthesis) If you add ( ), the statement tries to call the function instead of linking to it.

BUTTON TYPE CAN AFFECT CODING OF FUNCTION Buttons defined using the tag are always either a submit button or a reset button. They always submit/reset unless the function returns false; Buttons defined using the tag are also submit/reset buttons unless their type="button". These button buttons don’t do anything unless you link them to JavaScript. If you want a button button to submit, invoke the form’s submit( ) method $("frmTest").submit();

UPDATE YOUR SCRIPT Add function for reset Instead of clearing text box, place default text. Return false to prevent clearing of form

MY ADVICE... Use button buttons to invoke JavaScript functions that do things on the form but don’t submit or reset Use submit or reset types for buttons that should submit or reset even if additional processing is necessary. If JavaScript is off, submit and reset buttons should still function.

Open index.html. Note includes unit10.js Open unit10.js Discuss window.onload

NUMBER TYPE appears as a numeric up/down –clicking arrows changes by the step amount switch txtValue between number and range types and note differences max and min are set in unit10.js

DATE PICKER VALIDATION set the maximum and minimum date and note the date picker behavior differences between browsers

REQUIRED designates a field as required –if the field is blank when submitted, HTML displays an error message. –does not tell you the field is required until the form is actually submitted

FORMATTING INVALID FIELDS USING CSS observe the number change as we have valid and invalid numbers. in-html5/ in-html5/

HIDE/SHOW ERROR MARKER error markers are images displayed next to the fields create an.onblur event & the function onblur - occurs when you leave the field

VALIDATEUSERNAME will make the err.style visible if there is an error.

DISABLING/ENABLING INPUTS you can disable a control in the code as well.

NUMBER VALIDATION wire it up in the onload

SUMMARY OF THE NUMBER VALIDATION PRIOR SLIDE Create an onblur event handler in window.onload $("objID").onblur = validateMyField; //No () Extract the value from text box Turn off the error marker Deal with empty string if appropriate Check to ensure is numeric (if no keypress) Parse to a number Use IF for range checking If there's an error, change the title, show the marker

VALIDATION OF DATES The Date class has its idiosyncrasies ( gets converted to GMT) which can complicate date range validation. I’ve discovered the easiest way to compare to dates is to compare the string versions of those dates formatted yyyy-mm-dd

VALIDATE DATE Import the date entered by the user Before checking to see if the user’s entry is a valid date (isDate) I first replace all dashes ( - ) and periods (. ) with / (slash)

VALIDATE DATE Firefox and Chrome don’t like dates with dashes in these formats: or however if the dashes are slashes, the dates are acceptable. Replacing the dashes with slashes (behind the scenes) makes the form (JavaScript) more flexible for the user None of the browsers accept dates with periods (e.g ) Replacing the periods with slashes again makes the form more flexible

VALIDATEDATE If the format is not yyyy-mm-dd (use a regular expression), convert the input to a Date and reformat (import DateFormat.js) If the format is already correct, don’t mess with it (Date could screw it up)

REGULAR EXPRESSION FOR DATE remove the dash/slash,or dots globally, and replace them with a slash