Form Validation (with jQuery, HTML5, and CSS)

Slides:



Advertisements
Similar presentations
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?
Advertisements

CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Tutorial 6 Creating a Web Form
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.
Ch3: Introduction to HTML5 part 2 Dr. Abdullah Almutairi ISC 340 Fall 2014.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
JavaScript Forms Form Validation Cookies CGI Programs.
Session 2 Tables and forms in HTML Adapted by Sophie Peter from original document by Charlie Foulkes.
  Just another way to collect pieces together (like div, section)  Anything can be in there  Formatting just like all other elements.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
JavaScript Form Validation
1 Forms A form is the usual way that information is gotten from a browser to a server –HTML has tags to create a collection of objects that implement this.
Week 7. Lecture 3 PHP Forms. PHP forms In part 2 of this course, we discussed html forms, php form is similar. Lets do a quick recap of the things we.
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
Lecture # 6 Forms, Widgets and Event Handling. Today Questions: From notes/reading/life? Share Personal Web Page (if not too personal) 1.Introduce: How.
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.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
The Web Wizard’s Guide To JavaScript Chapter 3 Working with Forms.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
HTML Forms.
ASSIGNMENT POINTS DUE DATE: Monday NOV 30 JAVASCRIPT, INPUT VALIDATION, REGEX See 2 nd slide for Form See 3 rd next slide for the required features.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
JavaScript JavaScript is a programming language that web browsers understand. You can use it to make your web pages interactive by: Responding to user.
CIS 228 The Internet 12/6/11 Forms and Validation.
Tutorial 6 Creating a Web Form
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.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
>> Form Data Validation in JavaScript
Introduction to.
JavaScript, Sixth Edition
Checkboxes, Select boxes, Radio buttons,
Form Validation and AJAX
JavaScript Forms Adding User Input.
Web Programming– UFCFB Lecture 17
Intro to PHP & Variables
Working with Forms and Regular Expressions
Introduction to JavaScript
Objectives Explore web forms Work with form servers
A second look at JavaScript
>> Dynamic CSS Selectors
WEB PROGRAMMING JavaScript.
The Web Wizard’s Guide To JavaScript
HTML5 Level II Session III
Functions, Regular expressions and Events
Session IV Chapter 10 – How to Work with Forms and Data Validation
HTML and CSS MIS 2402 Jeremy Shafer Department of MIS
Form Validation (with jQuery, HTML5, and CSS)
Form Validation, Part 2 (with jQuery, HTML5, and CSS)
An Introduction to Animation
MIS JavaScript and API Workshop (Part 2)
Getting started with jQuery
Sending a text message (and more)
An introduction to jQuery
JavaScript objects, functions, and events
Introduction to JavaScript
An introduction to jQuery
Getting started with jQuery
An introduction to jQuery
Introduction to AJAX and JSON
Session III Chapter 10 – How to Work with Forms and Data Validation
Strings and Dates in JavaScript
Murach's JavaScript and jQuery (3rd Ed.)
HTML CS 4640 Programming Languages for Web Applications
Events Part III The event object.
Checkboxes, Select boxes, Radio buttons
Presentation transcript:

Form Validation (with jQuery, HTML5, and CSS) Maxwell Furman Department of MIS Fox School of Business Temple University

Let’s think about… Chapter 6 What is data validation? When we talk about “form validation” what is it we’re trying to do? What is one of the HTML5 controls that was created to simplify validation? What’s a regular expression?

Thinking some more… Where does our JavaScript/jQuery code run? Where does data validation need to take place? What has been our focus in this course? THE POINT IS: Today we are only focusing on half of what is needed for effective validation of user-provided data. We are focusing on the client-side half. This whole course focuses on just the client side of application development. Server-side resources are explored in MIS3502.

Revisiting HTML forms The HTML As seen in the browser… <h1>Registration Form For Sports</h1> <form action='http://misdemo.temple.edu/demo_post/handler/' method='post’ id=‘sports_form’> <label for='player_name'>Name</label> <input type='text' id='player_name' name='player_name'><br> <label for='surname'>Sur Name</label> <input type='text' id='surname' name='surname'><br> etc. etc. etc. <input type=‘button' name='button_register' value='register'> </form> What’s the point? stop “bad” data user might provide before it is ever sent! Recall the purpose of “action” and “method” in the form tag. Note that here we are specifying an input of type button. The form submit action will be initiated from the JavaScript/jQuery.

Options for form (data) validation #2. HTML5 added new controls and attributes intended to simplify form validation. It is taking the major browsers some time to catch up to the specification and support them all. We will focus on the most widely supported options. #3. CSS – all on its own – doesn’t provide form validation. But CSS3 did introduce special selectors related to form validation. HTML CSS JavaScript #1. JavaScript and jQuery have always been a good way to implement form validation. Of course, you need to write code to do specify the validation rules. Today we will only focus on option #1 Let’s see an example: validate_01.html

Things we will want to be able to do… Initiate form validation when a button is clicked Get text out of a input tag of type text Trim the text Validate the value of a selection list Validate the value of a selection radio button Validate the value of a checkbox Determine if the text matches a pattern… (like, an email address) Submit the form if the data is good, show an error message if it’s not. Place error messages in spans that are located next to the problematic data

.trim() A String's trim method can be used to remove leading and trailing spaces from a string. Specifically, "some string".trim() removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved. For example: var mytitle = " do androids dream? ".trim(); //var mytitle == 'do androids dream?' Another way to use .trim() is to attach it as a method to another jQuery method. This is called chaining. For example: var some_variable = (‘#some_tag_id’).val().trim(); See example: validate_02.html

The value of a select tag HTML Select Tag Using jQuery to determine the value of an HTML select tag is a two-step process. #1 - Give every <option> tag a value: <select id="idYourFav" name="favorite_color">             <option value="">Please choose</option>             <option value="red">Red</option>             <option value="orange">Orange</option>         </select> #2 – Get the value of the selected item with jQuery: $("#idYourFav").val() Warning! There are a lot of different ways to do this. What we’re demonstrating here is one of the more simple approaches. See example: validate_03.html

The value of a radio button HTML radio buttons. All have the same name. Get the value of the checked radio button with jQuery: $("input[name=favorite_color]:checked").val() See example: validate_04.html

The value of a checkbox HTML checkboxes. All have different names. Determining how many checkboxes are checked: $("#idYourFav input[type=checkbox]:checked").length See example: validate_05.html

Checking for a pattern (this one is tricky!) Regular expressions are hard! But, others have gone before you, and compiled many helpful examples. See: http://regexlib.com/ Define the RegEx pattern. Put it in a variable. var emailPattern = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b/; var email = $("#idYourEmail").val().trim(); if (!emailPattern.test(email) ) { $("#idYourEmail").next("span").html("Invalid email address."); isValid = false; } Get the data that you want to test. Trim it. Now use the .test() method of the RegEx pattern to validate the string. See example: validate_06.html

Programmer Joke Now, on to a challenge… “Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Now they have two problems.” -- Jamie Zawinski, Usenet 1997 Now, on to a challenge…

Options for form (data) validation #2. HTML5 added new controls and attributes intended to simplify form validation. It is taking the major browsers some time to catch up to the specification and support them all. We will focus on the most widely supported options. #3. CSS – all on its own – doesn’t provide form validation. But CSS3 did introduce special selectors related to form validation. HTML CSS JavaScript #1. JavaScript and jQuery have always been a good way to implement form validation. Of course, you need to write code to do specify the validation rules. Let’s see an example: validate_07.html

Things we will want to be able to do… Explore the (new) HTML5 controls and attributes now available. Understand why these controls have been slow to be adopted. Take a second look at Regular Expressions. Discover what a CSS pseudo class is, and how some can help with form validation. Return to jQuery, and be able to identify a handler.

HTML5 controls and attributes HTML5 controls for input data RED = Not supported in Safari How can you know? See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#Browser_compatibility Inconsistency is frustrating!…. ☹ The basic HTML5 attributes for working with forms Let’s see an example: validate_08.html Interactive examples: https://www.w3schools.com/html/html_form_input_types.asp

RegEx, again. Last class we saw jQuery’s RegEx option and the jQuery test method. Today, we see that HTML5 has RegEx support too! The new attributes to use are “title” and “pattern” See examples: validate_09.html So, if you needed to validate a telephone number, and you have a choice between validation that uses an HTML5 control of tel and a email regex pattern, which would you choose? Which would be better? Why?

CSS pseudo classes What’s a pseudo class? A CSS pseudo-class is a keyword added to a CSS selector that specifies a special state of the selected element(s). For example, :hover can be used to change a button's color when the user hovers over it. An easy example: /*apply the color when the mouse hovers over the div*/ div:hover { background-color: #39c631; } There are three pseudo classes that are most relevant to form validation: :required :valid :invalid See some examples here: validate_10.html and validate_11.html

A little bit more jQuery There are a number of jQuery methods that help us respond to events related to form validation. Here they are: focus(handler) blur(handler) change(handler) select(handler) submit(handler) We’re going to look at this one In case you haven’t noticed… we’ve been using the idea of a handler for a while now. A handler is function that executes in response to an event. A handler is always of the format: function () { /*commands go here*/ } See an example here: validate_12.html

Some thoughts… Support for the new HTML5 input controls has come a long way since they were first recommended by the W3C in 2014. However, we’ve seen today that their behavior is inconsistent across browser types. Likewise, they do not allow developers the full range of control that most solutions require. Prediction: As support for HTML5 controls continues to improve, they will eventually become mainstream. Opinion: The desired level of support is not quite there yet. Question: Even if these controls were totally, universally supported – would developers embrace them? Do end users want *every* application to be the same? Why? Why not?

Time for a challenge…