Web Programming Step by Step



Similar documents
CSE 154 LECTURE 11: REGULAR EXPRESSIONS

CSE 341 Lecture 28. Regular expressions. slides created by Marty Stepp

Web Programming Step by Step

Regular Expression Syntax

Regular Expressions Overview Suppose you needed to find a specific IPv4 address in a bunch of files? This is easy to do; you just specify the IP

Some Scanner Class Methods

JAVASCRIPT AND COOKIES

Lecture 18 Regular Expressions

Tutorial 6 Creating a Web Form. HTML and CSS 6 TH EDITION

CS 1133, LAB 2: FUNCTIONS AND TESTING

Variables, Constants, and Data Types

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Web Programming with PHP 5. The right tool for the right job.

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Embedded PHP. Web services vs. embedded PHP. CSE 190 M (Web Programming), Spring 2008 University of Washington

University Convocation. IT 3203 Introduction to Web Development. Pattern Matching. Why Match Patterns? The Search Method. The Replace Method

dtsearch Regular Expressions

CS106A, Stanford Handout #38. Strings and Chars

HTML Form Widgets. Review: HTML Forms. Review: CGI Programs

Lab 5 Introduction to Java Scripts

PHP Tutorial From beginner to master

Form Validation. Server-side Web Development and Programming. What to Validate. Error Prevention. Lecture 7: Input Validation and Error Handling

3.GETTING STARTED WITH ORACLE8i

SQL injection attacks SQL injection user input SQL injection SQL Command parameters Database account. SQL injection attacks Data Code

Inserting the Form Field In Dreamweaver 4, open a new or existing page. From the Insert menu choose Form.

Using Regular Expressions in Oracle

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Python Lists and Loops

Internet Technologies

Chapter 2: Elements of Java

CSE 203 Web Programming 1. Prepared by: Asst. Prof. Dr. Maryam Eskandari

Advanced Web Technology 10) XSS, CSRF and SQL Injection 2

Importing Lease Data into Forms Online

Now that we have discussed some PHP background

Version August 2016

Unit 6. Loop statements

CS412 Interactive Lab Creating a Simple Web Form

Real SQL Programming 1

Being Regular with Regular Expressions. John Garmany Session

XHTML Forms. Form syntax. Selection widgets. Submission method. Submission action. Radio buttons

Regular Expressions. Abstract

TCP/IP Networking, Part 2: Web-Based Control

LAB MANUAL CS (22): Web Technology

Sorting. Lists have a sort method Strings are sorted alphabetically, except... Uppercase is sorted before lowercase (yes, strange)

Introduction to Java

Content of this lecture. Regular Expressions in Java. Hello, world! In Java. Programming in Java

Designing for Dynamic Content

CSCI110: Examination information.

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

<option> eggs </option> <option> cheese </option> </select> </p> </form>

Regular Expression Denial of Service

Java Server Pages and Java Beans

Number Representation

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

PHP Authentication Schemes

Regular Expressions (in Python)

Kiwi Log Viewer. A Freeware Log Viewer for Windows. by SolarWinds, Inc.

Script Handbook for Interactive Scientific Website Building

JavaScript and Dreamweaver Examples

Regular Expressions. The Complete Tutorial. Jan Goyvaerts

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Passing 1D arrays to functions.

Iteration CHAPTER 6. Topic Summary

Regular Expressions. General Concepts About Regular Expressions

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

A Brief Introduction to MySQL

PHP. Introduction to Server-Side Programming. Charles Liu

JavaScript Introduction

Using PRX to Search and Replace Patterns in Text Strings

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Regular Expression Searching

Programming Languages CIS 443

Big Data and Analytics by Seema Acharya and Subhashini Chellappan Copyright 2015, WILEY INDIA PVT. LTD. Introduction to Pig

2- Forms and JavaScript Course: Developing web- based applica<ons

<?php if (Login::isLogged(Login::$_login_front)) { Helper::redirect(Login::$_dashboard_front); }

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Comp151. Definitions & Declarations

Critical Values for I18n Testing. Tex Texin Chief Globalization Architect XenCraft

Computational Mathematics with Python

Sample CSE8A midterm Multiple Choice (circle one)

FORMS. Introduction. Form Basics

Full URLs, specified in RFC 3986 have up to eight parts.

Outline. CS 112 Introduction to Programming. Recap: HTML/CSS/Javascript. Admin. Outline

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Lecture 5: Java Fundamentals III

Contents. 2 Alfresco API Version 1.0

Compiler Construction

All MySQL and PHP training students receive a copy of Apress' Beginning PHP and MySQL 5: From Novice to Professional and other related courseware.

Lecture 4. Regular Expressions grep and sed intro

Ad Hoc Advanced Table of Contents

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Building Java Programs

DESIGNING HTML HELPERS TO OPTIMIZE WEB APPLICATION DEVELOPMENT

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

FILE FORMAT OF PAYMENT ORDERS ACCEPTED BY CITIBANK EUROPE PLC FOR CITIBUSINESS DIRECT INTERNET BANKING

InternetVista Web scenario documentation

Computer Programming C++ Classes and Objects 15 th Lecture

Transcription:

Web Programming Step by Step Lecture 11 Form Validation Except where otherwise noted, the contents of this presentation are Copyright 2009 Marty Stepp and Jessica Miller. What is form validation? validation: ensuring that form's values are correct some types of validation: preventing blank values (email address) ensuring the type of values integer, real number, currency, phone number, Social Security number, postal address, email address, date, credit card number,... ensuring the format and range of values (ZIP code must be a 5-digit integer) ensuring that values fit together (user types email twice, and the two must match)

A real form that uses validation Client vs. server-side validation Validation can be performed: client-side (before the form is submitted) can lead to a better user experience, but not secure (why not?) server-side (in PHP code, after the form is submitted) needed for truly secure validation, but slower both best mix of convenience and security, but requires most effort to program

An example form to be validated <form action="http://foo.com/foo.php" method="get"> <div> City: <input name="city" /> <br /> State: <input name="state" size="2" maxlength="2" /> <br /> ZIP: <input name="zip" size="5" maxlength="5" /> <br /> <input type="submit" /> </div> </form> City: State: ZIP: Let's validate this form's data on the server... Basic server-side validation code $city = $_REQUEST["city"]; $state = $_REQUEST["state"]; $zip = $_REQUEST["zip"]; if (!$city strlen($state)!= 2 strlen($zip)!= 5) {?> <h2>error, invalid city/state submitted.</h2> <?php } basic idea: examine parameter values, and if they are bad, show an error message and abort validation code can take a lot of time / lines to write How do you test for integers vs. real numbers vs. strings? How do you test for a valid credit card number? How do you test that a person's name has a middle initial? (How do you test whether a given string matches a particular complex format?)

What is a regular expression? "/^[a-za-z_\-]+@(([a-za-z_\-])+\.)+[a-za-z]{2,4}$/" regular expression ("regex"): a description of a pattern of text can test whether a string matches the expression's pattern can use a regex to search/replace characters in a string regular expressions are extremely powerful but tough to read (the above regular expression matches email addresses) regular expressions occur in many places: Java: Scanner, String's split method (CSE 143 sentence generator) supported by PHP, JavaScript, and other languages many text editors (TextPad) allow regexes in search/replace Basic regular expressions "/abc/" in PHP, regexes are strings that begin and end with / the simplest regexes simply match a particular substring the above regular expression matches any string containing "abc": YES: "abc", "abcdef", "defabc", ".=.abc.=.",... NO: "fedcba", "ab c", "PHP",...

Wildcards:. A dot. matches any character except a \n line break "/.oo.y/" matches "Doocy", "goofy", "LooNy",... A trailing i at the end of a regex (after the closing /) signifies a case-insensitive match "/mart/i" matches "Marty Stepp", "smart fellow", "WALMART",... Special characters:, (), ^, \ means OR "/abc def g/" matches "abc", "def", or "g" There's no AND symbol. Why not? () are for grouping "/(Homer Marge) Simpson/" matches "Homer Simpson" or "Marge Simpson" ^ matches the beginning of a line; $ the end "/^<!--$/" matches a line that consists entirely of "<!--" \ starts an escape sequence many characters must be escaped to match them literally: / \ $. [ ] ( ) ^ * +? "/<br \/>/" matches lines containing <br /> tags

Quantifiers: *, +,? * means 0 or more occurrences "/abc*/" matches "ab", "abc", "abcc", "abccc",... "/a(bc)*/" matches "a", "abc", "abcbc", "abcbcbc",... "/a.*a/" matches "aa", "aba", "a8qa", "a!?_a",... + means 1 or more occurrences "/a(bc)+/" matches "abc", "abcbc", "abcbcbc",... "/Goo+gle/" matches "Google", "Gooogle", "Goooogle",...? means 0 or 1 occurrences "/a(bc)?/" matches "a" or "abc" More quantifiers: {min,max} {min,max} means between min and max occurrences (inclusive) "/a(bc){2,4}/" matches "abcbc", "abcbcbc", or "abcbcbcbc" min or max may be omitted to specify any number {2,} means 2 or more {,6} means up to 6 {3} means exactly 3

Character sets: [] [] group characters into a character set; will match any single character from the set "/[bcd]art/" matches strings containing "bart", "cart", and "dart" equivalent to "/(b c d)art/" but shorter inside [], many of the modifier keys act as normal characters "/what[!*?]*/" matches "what", "what!", "what?**!", "what??!",... What regular expression matches DNA (strings of A, C, G, or T)? "/[ACGT]+/" Character ranges: [start-end] inside a character set, specify a range of characters with - "/[a-z]/" matches any lowercase letter "/[a-za-z0-9]/" matches any lower- or uppercase letter or digit an initial ^ inside a character set negates it "/[^abcd]/" matches any character other than a, b, c, or d inside a character set, - must be escaped to be matched "/[+\-]?[0-9]+/" matches an optional + or -, followed by at least one digit What regular expression matches letter grades such as A, B+, or D-? "/[ABCDF][+\-]?/"

Escape sequences special escape sequence character sets: \d matches any digit (same as [0-9]); \D any non-digit ([^0-9]) \w matches any word character (same as [a-za-z_0-9]); \W any non-word char \s matches any whitespace character (, \t, \n, etc.); \S any non-whitespace What regular expression matches dollar amounts of at least $100.00? "/\$\d{3,}\.\d{2}/" Regular expressions in PHP (PDF) regex syntax: strings that begin and end with /, such as "/[AEIOU]+/" function preg_match(regex, string) preg_replace(regex, replacement, string) preg_split(regex, string) description returns TRUE if string matches regex returns a new string with all substrings that match regex replaced by replacement returns an array of strings from given string broken apart using the given regex as the delimiter (similar to explode but more powerful)

Regular expression example # replace vowels with stars $str = "the quick brown fox"; $str = preg_replace("/[aeiou]/", "*", $str); # "th* q**ck br*wn f*x" # break apart into words $words = preg_split("/[ ]+/", $str); # ("th*", "q**ck", "br*wn", "f*x") # capitalize words that had 2+ consecutive vowels for ($i = 0; $i < count($words); $i++) { if (preg_match("/\\*{2,}/", $words[$i])) { $words[$i] = strtoupper($words[$i]); } } # ("th*", "Q**CK", "br*wn", "f*x") notice how \ must be escaped to \\ PHP form validation w/ regexes $state = $_REQUEST["state"]; if (!preg_match("/[a-z]{2}/", $state)) {?> <h2>error, invalid state submitted.</h2> <?php } using preg_match and well-chosen regexes allows you to quickly validate query parameters against complex patterns