JavaScript Form Validation

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

The Web Wizards Guide To JavaScript Chapter 3 Working with Forms.
MWD1001 Website Production Using JavaScript with Forms.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Ch3: Introduction to HTML5 part 2 Dr. Abdullah Almutairi ISC 340 Fall 2014.
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
DT211/3 Internet Application Development JSP: Processing User input.
JavaScript Forms Form Validation Cookies CGI Programs.
JavaScript Lesson 1 TBE 540. Prerequisites  Before beginning this lesson, the learner must be able to… Create a basic web page using a text editor and/or.
Tutorial 14 Working with Forms and Regular Expressions.
ASP.NET Validating user input Validating user input on the client and/or server side 1ASP.NET Validating User Input.
Introduction to JavaScript Form Verification - Fort Collins, CO Copyright © XTR Systems, LLC Verifying Submitted Form Data with JavaScript Instructor:
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.
Client-Side programming with JavaScript 3
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Week 4  Using PHP with HTML forms  Form Validation  Create your own Contact form Please Visit:
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.
Department of Information Technology e-Michigan Web Development 0 HTML Form Creation in the Vignette Content Management Application.
Introduction to Programming the WWW I CMSC Summer 2003 Lecture 12.
Copyright ©2005  Department of Computer & Information Science Introducing DHTML & DOM: Form Validation.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Chapter 3 Using Validation Controls. What is a Validation Control? A control that validates the value in another control Renders as an HTML tag with an.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 9 Scott Marino.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
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.
Validation Controls. Validation Server Controls These are a special type of Web server control. They significantly reduce some of the work involved in.
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
Telerik Software Academy ASP.NET Web Forms Data Validation, Data Validators, Validation Groups Telerik Software Academy
CSCI 6962: Server-side Design and Programming Validation Tools in Java Server Faces.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
© 2000 – All Rights Reserved - Page 1 Introduction to JavaScript Programming Part Two.
JavaScript Lecture 6 Rachel A Ober
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.
PHP meets MySQL.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 3.
G053 - Lecture 16 Validating Forms Mr C Johnston ICT Teacher
C# Tutorial -1 ASP.NET Web Application with Visual Studio 2005.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
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.
Chapter 8 Collecting Data with Forms. Chapter 8 Lessons Introduction 1.Plan and create a form 2.Edit and format a form 3.Work with form objects 4.Test.
VALIDATION CONTROLS.  Validation Controls are primarily used to validate, or verify the data entered by user into a web form.  Validation controls attempt.
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.
The Web Wizard’s Guide To JavaScript Chapter 3 Working with Forms.
The Web Wizard’s Guide To JavaScript Chapter 3 Working with Forms.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Unit 10 – JavaScript Validation Instructor: Brent Presley.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Radio Buttons. Input/Form/Radio Group Use the dialog to enter label and values for the radio buttons.
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 Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
JavaScript, Sixth Edition
Chapter 5 Validating Form Data with JavaScript
Web Programming– UFCFB Lecture 17
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
The Web Wizard’s Guide To JavaScript
JavaScript Form Validation
Presentation transcript:

JavaScript Form Validation Supplemental Material

Sources for JavaScript Validation http://www.w3schools.com/js/js_form_validation.asp http://javascriptsource.com/forms/val-date.html http://www.tizag.com/javascriptT/javascriptform.php

JavaScript Form Validation Before an HTML form is submitted, JavaScript can be used to provide client-side data validation This is more user-friendly for the user than server-side validation because it does not require a server round trip before giving feedback If the form is not valid, the form is not submitted until the errors are fixed

Client-Side Validation Web Browser Web Server Application Server Database Server JavaScript Validation Application Validation HTTP Request HTTP Response JavaScript data validation happens before form is submitted Server-side application validation happens after the form is submitted to the application server

Client-Side vs Server-Side If creating a Web form, make sure the data submitted is valid and in the correct format Client-side validation gives the user faster feedback If possible, allow for server-side validation if the JavaScript is turned off Covered in future INFO courses

What to Validate on a Form? Form data that typically are checked by a JavaScript could be: were required fields left empty? was a valid e-mail address entered? was a valid date entered? was text entered in a numeric field? were numbers entered in an text field? did the number entered have a correct range?

onchange Validation To force the browser to check each field immediately, we add an onchange event to each of the <input> tags in the form For example: if we wanted to check if the value of a certain text field had a valid e-mail address we would add this: <input type="text" name="EMail" size="20" onchange="emailvalidation(this);" >

<form> onsubmit Event Your form must have a Submit button the user clicks when completing the form An onsubmit event will be raised and you should put this code in the <form> tag Call your event handler to go through and test each form field as needed If the event handler returns false, the form submission will be cancelled, if true the form will submit and the form action will be executed <form action="Process.php” onsubmit="return validate(this);” >

onsubmit Event Handler Pass the form object as the this parameter onsubmit=“return validate(this);” This function should create variables for each field that needs validation Set a inputvalid = true to begin with Use a series of if statements to perform each validation test, if test fails set inputvalid =false and set error message and/or alert message Finally, return inputvalid from the event handler

General Event Handler Structure function validate(form) { // Set each of needed form variables var input valid=true; var message=“Please fix the following errors \n”; if (!testFunction(text)) { // something is wrong // message =+ “new error \n”; // validinput= false; } if (!testFunction2(text)) { // something else is wrong // message =+ “new error \n”; // validinput= false; } // do each validation test if(!validinput) { alert (message); } return validinput; General Event Handler Structure

Required Fields What fields on a Web form should be required? Good usability practice suggests the form designer only make the user fill out necessary information This information may be required when sent to a database such as non-null data Additional good practice would mark which fields are required on the form Often marked with an * May spell out the “Required” or style differently

Testing for Required Entry Checking a textbox field could be done with a simple test for text.length == 0 Checking a select field to see if an option has been selected use selectedIndex>0 Place instruction text as the first <option> Checking a checkbox checked==true Checking a radiobutton need to loop through array and test each checked==true May want to always set a default radio button as selected=“selected”

Testing for Valid Input If a textbox asks for an email, test the text entered is a valid email If a textbox asks for a date, test the text entered is a valid date Very complex to text format and validity If a textbox asks for a zipcode, test the text entered is a valid zipcode

Regular Expressions You can use a symbol representation of a string value called a regular expression to test your input text There are many regular expressions available for common tests U.S. Phone: /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/ Email: /^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1} [0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$/ Currency: /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/

Example Regular Expression Test to see if text is a number Returns true if number, false otherwise Does the text match the pattern? // check if the text is a number function IsNumber(fData) { var reg = /^[0-9]+[\.]?[0-9]+$/; return reg.test(fData) }

Multiple Validations ??? A Web form field may need more than one validation test For example a textbox for age: It may be a required field It must be a number It must be in a range 0 - 120 Order your validation tests from general to most specific

Modularize Your Functions It is a good idea to write a separate function for each type of validation Generalize each function Place these frequently used validation functions in their own external JavaScript file

Fine Tune Error Messages Help users to successfully complete the Web form Provide hints on formatting by the fields on the form rather than wait for a submission error Be professional and helpful with the error messages Try to provide both error message by each field and summary text in an alert box

Testing the Web Form First test that required fields must be provided Then test fields that need valid input such as: Phone number Email address Dates Make sure error messages are appropriate and specific for each error

JavaScript Form Validation Summary JavaScript can be used for client-side data validation of a Web form Modularize validation functions to be reusable for future work Begin by making form user-friendly with instructions and hints Provide helpful error messages