Programming the Web VALIDATING FORM INPUT

Similar documents
JavaScript and XHTML. Prof. D. Krupesha, PESIT, Bangalore

CITS3403 Agile Web Development Semester 1, 2018

Outline. Lecture 4: Document Object Model (DOM) What is DOM Traversal and Modification Events and Event Handling

CSC Web Programming. JavaScript Browser Objects

CSI 3140 WWW Structures, Techniques and Standards. Browsers and the DOM

Chapter 11 Objectives

JavaScript: Events, the DOM Tree, jquery and Timing

Recall: Document Object Model (DOM)

Document Object Model (DOM) A brief introduction. Overview of DOM. .. DATA 301 Introduction to Data Science Alexander Dekhtyar..

Walking the DOM Tree. Lecture Outline. An example XHTML page. CSE 190 M (Web Programming), Spring 2008 University of Washington

By the end of this section of the practical, the students should be able to:

DOM Primer Part 2. Contents

The attributes property of an element node returns a list of attribute nodes. It s called a named node map

SYBEX Web Appendix. DOM Appendix: The Document Object Model, Level 1

PES DEGREE COLLEGE BANGALORE SOUTH CAMPUS 1 K.M. before Electronic City, Bangalore WEB PROGRAMMING Solution Set II

INLEDANDE WEBBPROGRAMMERING MED JAVASCRIPT INTRODUCTION TO WEB PROGRAMING USING JAVASCRIPT

Forms, Form Events, and Validation. Bok, Jong Soon

AJAX: The Basics CISC 282 March 25, 2014

1$ 5 ! $ 6 4 * Source: 0 "!*! 0! * 8 97 ?!$ 5 0 *! 4! $ 0 : * ' () 7;7 7<7

UNIT - III. Every element in a document tree refers to a Node object. Some nodes of the tree are

JavaScript Handling Events Page 1

AJAX: The Basics CISC 282 November 22, 2017

COMP519 Web Programming Lecture 16: JavaScript (Part 7) Handouts

INTRODUCTION TO WEB DEVELOPMENT AND HTML. Lecture 13: Intro to JavaScript - Spring 2011

JavaScript!= Java. Intro to JavaScript. What is JavaScript? Intro to JavaScript 4/17/2013 INTRODUCTION TO WEB DEVELOPMENT AND HTML

Catching Events. Bok, Jong Soon

Introduction to DHTML

INTERNET & WEB APPLICATION DEVELOPMENT SWE 444. Fall Semester (081) Module 4 (VII): XML DOM

Ajax. Ronald J. Glotzbach

Best Practices Chapter 5

Web Programming CS333/CS614

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

1. Cascading Style Sheet and JavaScript

Javascript. Many examples from Kyle Simpson: Scope and Closures

blink.html 1/1 lectures/6/src/ form.html 1/1 lectures/6/src/

DOM. Ajax Technology in Web Programming. Sergio Luján Mora. DLSI - Universidad de Alicante 1. API for accessing and manipulating HTML documents

Then there are methods ; each method describes an action that can be done to (or with) the object.

JavaScript CSCI 201 Principles of Software Development

Javascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

LAB MANUAL SUBJECT: WEB TECHNOLOGY CLASS : T.E (COMPUTER) SEMESTER: VI

Q1. What is JavaScript?

JavaScript: Introduction to DOM and Attaching Handlers

CISH-6510 Web Application Design and Development. Overview of JavaScript. Overview

Basics of JavaScript. Last Week. Needs for Programming Capability. Browser as Development Platform. Using Client-side JavaScript. Origin of JavaScript

Lesson 3. Form By Raymond Tsang. Certificate Programme in Cyber Security

IS 242 Web Application Development I

HTML and JavaScript: Forms and Validation

Overview of Forms. Forms are used all over the Web to: Types of forms: Accept information Provide interactivity

New Media Production Lecture 7 Javascript

CIW 1D CIW JavaScript Specialist.

8/19/2018. Web Development & Design Foundations with HTML5. Learning Objectives (1 of 2) Learning Objectives (2 of 2) What is JavaScript?

Session 16. JavaScript Part 1. Reading

New Perspectives on Creating Web Pages with HTML. Tutorial Objectives

Document Object Model (DOM)

Interactor Tree. Edith Law & Mike Terry

PIC 40A. Review for the Final. Copyright 2011 Jukka Virtanen UCLA 1 06/05/15

Web Development & Design Foundations with HTML5

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2017)

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

HTML 5 and CSS 3, Illustrated Complete. Unit L: Programming Web Pages with JavaScript

INDEX SYMBOLS See also

Ajax in Practice by Dave Crane Bear Bibeault Jord Sonneveld with Ted Goddard, Chris Gray, Ram Venkataraman and Joe Walker

XML in the Development of Component Systems. The Document Object Model

JavaScript s role on the Web

Overview. Event Handling. Introduction. Reviewing the load Event. Event mousemove and the event Object. Rollovers with mouseover and mouseout

JavaScript and the DOM MIS Konstantin Bauman. Department of MIS Fox School of Business Temple University

LA TROBE UNIVERSITY SEMESTER ONE EXAMINATION PERIOD. Subject Name: WEB DEVELOPMENT CAMPUS AW BE BU MI SH ALLOWABLE MATERIALS

Fundamentals of Website Development

HTML Forms. By Jaroslav Mohapl

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Chapter 16. JavaScript 3: Functions Table of Contents

Javascript Lecture 23

CSE 154 LECTURE 10: THE DOM TREE

Session 6. JavaScript Part 1. Reading

XML: a "skeleton" for creating markup languages you already know it! <element attribute="value">content</element> languages written in XML specify:

Web Development & Design Foundations with HTML5, 8 th Edition Instructor Materials Chapter 14 Test Bank

XML CSC 443: Web Programming

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

HTML Tables and Forms. Outline. Review. Review. Example Demo/ Walkthrough. CS 418/518 Web Programming Spring Tables to Display Data"

710 Index Attributes, 127 action attribute, 263 assigning, bottom attribute, domain name attribute, 481 expiration date attribute, 480 8

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

The Document Object Model (DOM) is a W3C standard. It defines a standard for accessing documents like HTML and XML.

Session 17. JavaScript Part 2. W3C DOM Reading and Reference. Background and introduction.

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

COMS 469: Interactive Media II

Client-side Techniques. Client-side Techniques HTML/XHTML. Static web pages, Interactive web page: without depending on interaction with a server

CSC Javascript

Photo from DOM

Document Object Model (DOM)

LAB Test 1. Rules and Regulations:-

Lesson 7: If Statement and Comparison Operators

JavaScript 3. Working with the Document Object Model (DOM) and DHTML

DOM Interface subset 1/ 2

Web Design. Lecture 6. Instructor : Cristina Mîndruță Site : Cristina Mindruta - Web Design

What Is JavaScript? A scripting language based on an object-orientated programming philosophy.

HTML Tables and. Chapter Pearson. Fundamentals of Web Development. Randy Connolly and Ricardo Hoar

Events: another simple example

HTML forms and the dynamic web

<form>. input elements. </form>

Transcription:

VALIDATING FORM INPUT One of the common uses of JavaScript is to check the values provided in forms by users to determine whether the values are sensible. When a user fills in a form input element incorrectly and a JavaScript event handler function detects the error, the function should produce an alert message indicating the error to the user and informing the user of the correct format for the input. The form in the next example includes the two password input elements, along with Reset and Submit buttons. The JavaScript function that checks the passwords is called either when the Submit button is pressed, using the onsubmit event to trigger the call, or when the second text box loses focus, using the blur event. The function performs two different tests. First, it determines whether the user typed the initial password (in the first input box) by testing the value of the element against the empty string. If no password has been typed into the first field, the function calls alert to produce an error message and returns false. The second test determines whether the two typed passwords are the same. If they are different, once again the function calls alert to generate an error message and returns false. If they are the same, it returns true. //pswd_chk.html <head> <title>password Checking</title> <script type = "text/javascript" src = "pswd_chk.js"> </script> <body onsubmit="return chkpass( );"> <h3>password Input</h3> <form action=" "> Your Password: <input type="password" id="initial" size="10"/> Verify Password: <input type="password" id="final" size="10"/> <input type="reset" name="reset"/;> <input type="submit" name="submit"/> //pswd_chk.js function chkpass( ) { var init=document.getelementbyid("initial"); var fin=document.getelementbyid("final"); if(init.value=="") { alert("please enter Password"); init.focus( );

if(fin.value=="") { alert("please enter Confirm Password"); fin.focus( ); if(init.value!=fin.value) { alert("the passwords you entered do not match... Try Again"); init.focus( ); Output: We now consider an example that checks the validity of the form values for a name and phone number obtained from text boxes. The pattern for matching names [LastName, FirstName, MiddleName] is as follows: /^[A-Z][a-z]+,?[A-Z][a-z]+,?[A-Z]\.?$/ The pattern for phone numbers is as follows: /^d{3-\d{8$/ The following is the XHTML document, validator.html, that displays the text boxes for a customer s name and phone number: //validator.html <head> <title>name and Phone check</title> <script type = "text/javascript" src = "validator.js"> </script> Prepared By: Vijay Kumar Y <form action="" onsubmit="return chkname();"> Name:<input type="text" id="custname"/>(last name, first name, middle initial)<br> <input type="reset" id="reset"/> <input type="submit" id="submit"/>

//validator1.html <head> <title>name and Phone check</title> <script type = "text/javascript" src = "validator.js"> </script> Prepared By: Vijay Kumar Y <form action="" onsubmit="return chkphone();"> Phone:<input type="text" id="custphone"/> (ddd-dddddddd)<br> <input type="reset" id="reset"/> <input type="submit" value="submit"/> // validator.js function chkname() { var myname = document.getelementbyid("custname"); var pos = myname.value.search(/^[a-z][a-z]+,?[a-z][a-z]+,?[a-z]\.?$/); alert("the name you entered is not in the form last-name, first-name, middle-initial Please go and fix your name"); myname.focus(); myname.select(); function chkphone() { var myphone = document.getelementbyid("custphone"); var pos = myphone.value.search(/^\d{3-\d{8$/); alert("the phone you entered is not in the correct form.the correct form is:ddddddddddd "); myphone.focus(); myphone.select();

THE DOM 2 EVENT MODEL The DOM 2 model is a modularized interface. One of the DOM 2 modules is Events, which includes several sub-modules. The ones most commonly used are HTMLEvents and MouseEvents. The interfaces and events defined by these modules are as follows: EVENT PROPAGATION: A browser which understands DOM, on receiving the XHTML document from the server, creates a tree known as document tree. The tree constructed consists of elements of the document except the HTML The root of the document tree is document object itself The other elements will form the node of the tree In case of DOM2, the node which generates an event is known as target node Once the event is generated, it starts the propagation from root node During the propagation, if there are any event handlers on any node and if it is enabled then event handler is executed The event further propagates and reaches the target node. When the event handler reaches the target node, the event handler gets executed After this execution, the event is again re-propagated in backward direction During this propagation, if there are any event handlers which are enabled, will be executed. The propagation of the even from the root node towards the leaf node or the target node is known as capturing phase. The execution of the event handler on the target node is known as execution phase. This phase is similar to event handling mechanism in DOM 0 The propagation of the event from the leaf or from the target node is known as bubbling phase All events cannot be bubbled for ex: load and unload event If user wants to stop the propagation of an event, then stop propagation has to be executed. EVENT REGISTRATION: In case of DOM2, the events get registered using an API known as addeventlistener The first arg is the eventname. Ex: click, change, blur, focus The second arg is the event handler function that has to be executed when there is an event The third arg is a Boolean argument that can either take a true or false value If the value is true, it means event handler is enabled in capturing phase If the event value if off (false), then event handler is enabled at target node The addeventlistener method will return event object to eventhandler function. The event object can be accessed using the keyword Event The address of the node that generated event will be stored in current target, which is property of event object

AN EXAMPLE OF THE DOM 2 EVENT MODEL The next example is a revision of the validator.html document and validator.js script from previous example, which used the DOM 0 event model. Because this version uses the DOM 2 event model, it does not work with IE8. //validator1.html <head> <title>illustrate form input validation with DOM 2</title> <script type = "text/javascript" src = "validator2.js"></script> <form action="" onsubmit=""> <input type="text" id="custname"/>name(last name, first name, middle initial)<br> <input type="reset" /> <input type="submit" id="submitbutton"/> //validator2.html <head> <title>illustrate form input validation with DOM 2</title> <script type = "text/javascript" src = "validator2.js"></script> <form action="" onsubmit=""> <input type="text" id="custphone"/>phone (ddd-dddddddd) <input type="reset" /> <input type="submit" id="submitbutton"/> //validator2.js function chkname(event) { var myname = event.currenttarget; var pos = myname.value.search(/^[a-z][a-z]+,?[a-z][a-z]+,?[a-z]\.?$/); alert("the name you entered is not in the form last-name, first-name, middle-initial"); myname.focus(); myname.select();

function chkphone(event) { var myphone = event.currenttarget; var pos = myphone.value.search(/^\d{3-\d{8$/); alert("the phone you entered is not in the correct form.the correct form is: ddd-dddddddd "); myphone.focus(); myphone.select(); var c = document.getelementbyid("custname"); var p = document.getelementbyid("custphone"); c.addeventlistener("change",chkname,false); p.addeventlistener("change",chkphone,false); THE navigator OBJECT The navigator object indicates which browser is being used to view the XHTML document. The browser s name is stored in the appname property of the object. The version of the browser is stored in the appversion property of the object. These properties allow the script to determine which browser is being used and to use processes appropriate to that browser. The following example illustrates the use of navigator, in this case just to display the browser name and version number: //navigate.html file <head> <title>navigator</title> <script type = "text/javascript" src = "navigate.js"> </script> <body onload = "navproperties()"> //navigate.js file function navproperties() { alert("the browser is: " + navigator.appname + "\n" + "the version number is: " + navigator.appversion + "\n"); Output

DOM TREE TRAVERSAL AND MODIFICATION DOM TREE TRAVERSAL The parentnode property has the DOM address of the parent node of the node through which it is referenced. The childnodes property is an array of the child nodes of the node through which it is referenced. The previoussibling property has the DOM address of the previous sibling node of the node through which it is referenced. The nextsibling property has the DOM address of the next sibling node of the node through which it is referenced. The firstchild and lastchild properties have the DOM addresses of the first and last child nodes, respectively, of the node through which they are referenced. The nodetype property has the type of the node through which it is referenced. DOM TREE MODIFICATION A number of methods allow JavaScript code to modify an existing DOM tree structure. The insertbefore(newchild, refchild) method places the newchild node before the refchild node. The replacechild(newchild, oldchild) method replaces the oldchild node with the newchild node. The removechild(oldchild) method removes the specified node from the DOM structure. The appendchild(newchild) method adds the given node to the end of the list of siblings of the node through which it is called.