Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Dynamic Forms Designing Forms – Forms Basics
Modifying existing content Adding/Removing content on a page using jQuery.
Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
Downloading and Installing AutoCAD Architecture 2015 This is a 4 step process 1.Register with the Autodesk Student Community 2.Downloading the software.
Instructions on how to apply electronically to: Summer College for High School Students (U.S. Students or International Students) Summer Academy for High.
Logins  You will need PHP to test this code, all modern web hosting companies will provide this, Lehigh does not.  I've given you an account on des170.com:
Lesson 8 Creating Forms with JavaScript
CST JavaScript Validating Form Data with JavaScript.
WEBINAR SERIES: ACCESSIBLE INTERACTIVE DOCUMENTS Week 3: Accessible Web Forms Norman Coombs
JavaScript Form Validation
Week 4  Using PHP with HTML forms  Form Validation  Create your own Contact form Please Visit:
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
4-Sep-15 HTML Forms Mrs. Goins Web Design Class. Parts of a Web Form A Form is an area that can contain Form Control/Elements. Each piece of information.
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Jquery IS JQuery Jquery Example of mouseover event that shows a submenu when menu selected: $(‘#menu’).mouseover(function() { $(‘#submenu’).show();
JQuery and Forms – Part I. Learning Objectives By the end of this lecture, you should be able to: – Retrieve the values entered by a user into a form.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
Encoding, Validation and Verification Chapter 1. Introduction This presentation covers the following: – Data encoding – Data validation – Data verification.
Washington Campus Compact New Time Log Database Note to users: You should use Internet Explorer to use this database. In other programs (i.e. Firefox)
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
PHP meets MySQL.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 3.
Lecture # 6 Forms, Widgets and Event Handling. Today Questions: From notes/reading/life? Share Personal Web Page (if not too personal) 1.Introduce: How.
G053 - Lecture 16 Validating Forms Mr C Johnston ICT Teacher
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
Dreamweaver MX. 2 Overview of Templates n Forms enable you to collect data from ______. n A form contains ________ such as text fields, radio buttons,
Working with Files. Learning Objectives By the end of this lecture, you should be able to: – Examine a file that contains code with unfamiliar material,
CSC 240 (Blum)1 Introduction to Data Entry, Queries and Reports.
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.
NYS Division of Homeland Security And Emergency Services (DHSES) E-Grants Tutorial Creating an Application for the EOC RFP To access DHSES E-Grants you.
Working with Strings. Learning Objectives By the end of this lecture, you should be able to: – Appreciate the need to search for and extract information.
INSTRUCTOR: JOAN RABIDEAU Unit 5 ~ CS119 is the fastest way to reach me to get assistance and support! AIM – joanlrabideau.
Copyright © Texas Education Agency, All rights reserved.1 Web Technologies Website Forms / Data Acquisition.
Events Part I Mouse Events. Learning Objectives By the end of this lecture, you should be able to: – Understand what is meant by an ‘event’ – Appreciate.
Instructions on how to apply electronically to: Summer College for High School Students (U.S. Students or International Students) Summer Academy for High.
FORM VALIDATION (Java Script ). Required Fields A required field in a form is a field that MUST have at least some content before the form will be processed.
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
How to Complete FAFSA (Free Application for Federal Student Aid)
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
Web Forms. Web Forms: A form allows our web visitors to submit information to us. Some examples uses for forms are to let the web user contact us, fill.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
How To Make Easysite Forms By Joshua Crawley Contact:
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Ashima Wadhwa Java Script And Forms. Introduction Forms: –One of the most common Web page elements used with JavaScript –Typical forms you may encounter.
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 – If/Else contd
JavaScript Controlling the flow of your programs with ‘if’ statements
IS1500: Introduction to Web Development
Chapter 5 Validating Form Data with JavaScript
Computer Programming I
Retrieving information from forms
Web Programming– UFCFB Lecture 17
Active Orders Supplier Administrator Training Getting Started Activities This training presentation describes the Getting Started activities that will.
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
Selection Statements Chapter 4 Attaway MATLAB 4E.
Removing events Stopping an event’s normal behavior
Working with ‘checked’ items Checkboxes and Radio buttons
Selection Statements Chapter 4 Attaway MATLAB 5E.
Events Part III The event object.
Retrieving information from forms
Checkboxes, Select boxes, Radio buttons
Presentation transcript:

Form Validation

Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why form validation is important – Do basic form validation using jQuery including: Ensuring required text fields have information in them Ensuring that at least one of a (required) group of radio buttons was selected – Understand how and when to use ‘ return false ’

Random topic: The return false; statement Here is an important piece of code to know: return false; This statement immediately causes the function to end. Any remaining code that is present in the fucntion will not be executed. For example: function doStuff() { var num1=5, num2=5; var sum = num1+num2; return false; //function immediately ends alert(sum); //this statement will NOT be executed } We might ‘ return false ’ in a situation where we have a function that works with a form. For example, BEFORE we do all of the ‘stuff’ required to process the form, we frequently want to make sure the form has been properly filled out. The process of checking a form to make sure that all required fields are properly filled out is called “form validation”. If there is something that is not properly done in the form, we will typically want to let the user know of the problem. But more importantly, we would then want to STOP the remainder of the function dealing with the form from proceeding. For example, if the user forgot to enter their address, we would not want to continue with a function that adds the user to a mailing list. It is at such a point that we would use return false;

Example using return false; Suppose we had a form in which we ask the user to enter their address into a text field called ‘ txt Address ’. We would first check to make sure that the field is not empty. If it is empty, we would let them know that they forgot to complete that field. At that point, we would use ‘return false ’ to end the function. function addToMailingList() { var Address = $('#txt Address').val(); if ( Address == '') //if the field is empty { alert('You forgot to enter your address!'); return false; //ENDS the function right here! } //CODE TO ADD THE ADDRESS TO THE DATABASE WOULD GO HERE... //However, if we HAD executed ‘return false’, then the code //we would never reach this point }

Form Validation Example A very important skill in creating web forms is the ability to “validate” the form. That is, to ensure that the form has been properly and/or completely filled out. Examples of form validation include: Making sure that the user has filled out all required fields (name, phone number, etc) Making sure that the user has chosen at least one of a series of radio buttons that ask a question that requires an answer. (E.g. “What is your preferred t-shirt size?”) Making sure that the user has not entered inappropriate data in a field. – This is a pretty important one nowadays. One method used by hackers to break into websites is to enter scripting code into a text field and ‘hijack’ the script to access resources. Therefore, for secure websites, all forms are typically validated to make sure that key programming symbols (semicolons, quotes, asterisks and other important characters) are not present where they do not belong. This is why you will sometimes submit a form only to have it come back and say something like: “Please enter only numbers for your phone number. Please do not enter any other characters such as parentheses or dashes”. For our example, let’s write some code to make sure that a user has not left a textfield called ‘ txtName ’ blank. If they did neglect to enter their name, we will output an alert box letting them know of the error, and highlight that textfield on the page. Here is the hypothetical text field: Name? Now our script: var userData = $('#txtName').val(); if ( userData == '' ) //if it is an empty string, this means the user didn’t enter anything { alert('Please be sure to enter your name.'); $('#txtName').css('background-color', 'yellow'); //Highlight the empty field return false; //since the form was incomplete, do not continue to process it }

Form Validation Example Let’s write some code to make sure that a user has not left the ‘name’ and ‘ ’ text fields blank. We will also make sure that they chose one option for their ‘t-shirt size’. For all of these cases, we will output an alert box letting them know which item they neglected to complete. We will also highlight the label preceding the input that was missed. The function connected to the form checks to see that the form was properly completed and all required items were filled out. Start by studying the form and then study the script. FILE: form_validation_simple.htm

REVIEW: Checking for ‘UN-checked’ Recall our discussion on determining whether or not something such as a checkbox has NOT been checked: If the following code will determine if something IS checked: if ( $('#chkMushrooms').prop('checked') ) alert('You want mushrooms'); Then we can use the ‘!’ operator, to determine if something has NOT been checked: if ( ! $('#chkMushrooms').prop('checked') ) alert('You do NOT want mushrooms');

Checking for ‘UN-checked’ with Radio Buttons We’ve discussed how to determine whether or not a checkbox has been checked. But what if we want to do the same thing with a group of radio buttons? In fact, another very common task in form validation is to check to see if the user neglected to check any of a group of radio buttons. Suppose you have the following radio buttons: Preferred T-Shirt Color? Red Blue Green If we want to verify that the user DID choose one of the three buttons, we need to make sure that all three of the buttons are NOT UN-checked. (I realize there are a lot of negatives in that sentence! – So take a moment to make sure you understand what we are trying to do…) Here is the code to see if ALL 3 buttons are unchecked: if ( ! $('#radRed').prop('checked') && ! $('#radBlue').prop('checked') && ! $('#radGreen').prop('checked') ) { alert('You must choose a color!'); $('lblColor').css('background-color', 'yellow'); return false; }

More elaborate form validation example Here is a slightly more elaborate example of form validation: FILE: museum_admission_validation.htm