Validating user input Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.

Slides:



Advertisements
Similar presentations
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Chapter 14 Perl-Compatible Regular Expressions Part 1.
Data Manipulation & Regular Expressions CSCI 215.
Database Connectivity in PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
Introduction to PHP (Part-1) Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
IS 1181 IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions.
File uploading in PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
1.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Last Updated March 2006 Slide 1 Regular Expressions.
Strings and Arrays. String Is a sequence of characters. Example: “hello”, “how are you?”, “123”,and are all valid string values.
Deleting and Updating Records in MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
Pattern matching with regular expressions A common file processing requirement is to match strings within the file to a standard form, e.g. address.
PHP Workshop ‹#› Data Manipulation & Regex. PHP Workshop ‹#› What..? Often in PHP we have to get data from files, or maybe through forms from a user.
Chapter 3 Manipulating Strings PHP Programming with MySQL 2nd Edition
Web Application and Development Digital Media Department Unit Credit Value : 4 Essential Learning time : 120 hours Digital Media.
History, Navigator, Screen and Form Objects Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,
Adding Image, audio and video files to web pages Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Regular Expression (continue) and Cookies. Quick Review What letter values would be included for the following variable, which will be used for validation.
PHP Strings. Outline o String Variables In PHP o The Most Used Functions In PHP o Arrays Functions o Transforming String o Comparing Strings o Searching.
Strings in PHP Working with Text in PHP Strings and String Functions Mario Peshev Technical Trainer Software University
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
13/2/12 Lecture 6. Functions 2 types: – Built in functions – Custom defined functions Functions minimize the amount of repetition of code. function consists.
Modeling web applications
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
PHP with Regular Expressions Web Technologies Computing Science Thompson Rivers University.
PHP: A RRAYS, S TRINGS, AND F ILES CSCI 297 Scripting Languages - Day Three.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Retrieving data from MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
Regular Expressions in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
1 PHP Intro PHP Strings After this lecture, you should be able to: Manipulate and Output PHP Strings: Manipulate and Output PHP Strings: Single- or Double-quoted.
12. Regular Expressions. 2 Motto: I don't play accurately-any one can play accurately- but I play with wonderful expression. As far as the piano is concerned,
PHP’s Regular Expression Functions (Perl Compatible) Examples taken from: Beginning PHP 5 and MySQL 5 From Novice to Professional.
JavaScript III ECT 270 Robin Burke. Outline Validation examples password more complex Form validation Regular expressions.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
HTML FORMS Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan. 1.
INT222 – Internet Fundamentals Week 11: RegExp Object and HTML5 Form Validation 1.
1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1.
HTML 5 Form elements Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
HTML Links and navigation Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Lecture 19 Strings and Regular Expressions
Regular Expressions in Perl
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
The Selection Structure
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Passing variables between pages
String functions
Data Manipulation & Regex
Web Programming Language
String.
Topics Basic String Operations String Slicing
PHP PART 2.
Topics Basic String Operations String Slicing
String functions
Topics Basic String Operations String Slicing
Presentation transcript:

Validating user input Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1

Summary of the previous lecture Super Global variables Passing form data Passing data with sessions Basharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan. 2

Outline Regular expressions in PHP Validating user input at server String functions Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 3

1. Regular expressions in PHP A regular expression is a concise notation to describe patterns in strings Regular expressions provide the foundation for describing or matching data according to defined syntax rules – Example: |^[0-9]{2}-[0-9]{2}-[0-9]{4}$| Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 4

1. Regular expressions in PHP… |^[0-9]{2}-[0-9]{2}-[0-9]{4}$| Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 5 Start and end of RE Sub pattern Start matching from the start Match the end of the string Sub pattern with fixed character Allowed characters length [0-9] {2}

1. Regular expressions in PHP… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 6 Start and end of the RE: – optional, || Sub-patterns: – range of allowed characters – Allowed length Sub-patterns with fixed character:

1. Regular expressions in PHP… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 7 Matching from the start: – Matching till end: – For exact match we should use both ^ and $ Pattern exists if do not match from start Pattern exists if do not match till end

1.1 Notations for RE ^: match strings that start with the given pattern $: match strings that end with the given pattern -: means a range of characters [ ]: makes a class of characters [^ ]: negates the class of character Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 8

1.1 Notation for RE… Quantifiers: {n}: matches a character, class or sub- pattern for n times { n, m}: matches a character, class or sub- pattern for minimum n times and maximum m times Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 9

1.1 Notation for RE… ?: matches the character, class or sub- pattern 0 or 1 time – equal to {0,1} +: matches the character, class or sub- pattern 1 or more times – equals to {1, } *: matches the character, class or sub- pattern 0 or any number of time – equals {0, } Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 10

1.1 Notation for RE… Predefined character ranges: \d: means exactly as [0-9] \D: means exactly as [^0-9] \w: means exactly as [a-zA-Z0-9] Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 11

1.1 Notation for RE… RE examples: Validating date: – |^\d{2}-\d{2}-\d{4}$| Validating CNIC: – |^\d{5}-\d{7}-\d{1}$| Validating – Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 12

1.1 Notation for RE… Validating name: – |^[a-zA-Z ]{5,25}$| Validating Password: – must contain – Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 13

2. Validating user’s input preg_match(): – searches a string for a specific pattern – returns TRUE if it exists and FALSE otherwise – preg_match(“pattern”,$string); Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 14

2. Validating user’s input Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 15 name cnic dob Post, action.php

2. Validating user’s input Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 16 Receiving values Validating name

2. Validating user’s input Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 17 CNIC DoB

3. String functions in PHP strlen(): – Returns the length of the string – strlen($string); strcmp(): – Compares two strings – Returns 0 if strings are equal, 1 if first string is greater and -1 if second is greater – strcmp($string1,$string2); Strcasecmp(): – Compares two strings in case insensitive manner – strcasecmp($string1,$string2); Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 18

3. String functions in PHP… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 19 name pass pass1 Method=post

3. String functions in PHP… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 20 Getting variables Using strlen()

3. String functions in PHP… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 21 Password is short

3. String functions in PHP… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 22 Compares pass and pass1

3. String functions in PHP… strtolower(): – Convert a string in lower case – strtolower($string); strtoupper(): – Convert a string in upper case – strtoupper($string); ucfirst(): – Convert the first character of a string to upper case – ucfirst($string); ucwords(): – Convert the first character of each word in a string to upper case – ucfirst($string); Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 23

3. String functions in PHP… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 24 Converts name to lowercase Converts name to uppercase Using ucwords()Using ucfirst()

3. String functions in PHP… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 25 Lowercase uppercase ucfirst() ucwords()

3. String functions in PHP… strpos(): – finds the position of the first case-sensitive occurrence of a substring in a string – strpos($string,sub-string); strrpos(): – finds the position of the last case-sensitive occurrence of a substring in a string – strrpos($string,sub-string); substr_count(): – returns the number of times one string occurs within another – substr_count($string,sub-string); Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 26

3. String functions in PHP… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 27 Finding first occurrence of ‘a’ Last occurrence of ‘a’ Finding number of occurrences of ‘a’

3. String functions in PHP… Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 28 First occurrence of ‘a’ Last occurrence of ‘a’ Number of occurrences of ‘a’

Summary Writing regular expression in PHP Validating user’s input String functions Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 29

References Chapter 9, “Beginning PHP and MySQL” by W. Jason Gilmore, Apress publisher, 4th edition; 2010, ISBN-13 (electronic): Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 30