Week 14 - Validatinguserinput
Week 14 - Validatinguserinput
2
Validating User Input
3
Today’s Lecture Outline
• Regular expressions in PHP
• Validating user input at server
• String functions
4
1. Regular expressions in PHP
• Regular expressions are sequence or pattern of
characters itself. They provide the foundation
for pattern-matching functionality
• 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}$|
5
1. Regular expressions in PHP
• Using regular expression you can search a
particular string inside a another string, you can
replace one string by another string and you can
split a string into many chunks.
• PHP offers functions specific to two sets of
regular expression functions, each corresponding
to a certain type of regular expression. You can
use any of them based on your comfort.
• POSIX Regular Expressions
• PERL Style Regular Expressions
6
1. Regular expressions in PHP…
Sub pattern with fixed character
|^[0-9]{2}-[0-9]{2}-[0-9]{4}$|
Sub pattern Sub pattern Sub pattern
Start matching
Match the end of
from the start [0-9] {2} the string
Allowed length
characters
7
1. Regular expressions in PHP…
• Brackets
• Brackets ([]) have a special meaning when used
in the context of regular expressions. They are
used to find a range of characters.
• [0-9] It matches any decimal digit from 0 through 9.
• Brackets: [a-Z], [A-Z], [a-z], [0-9]
8
1. Regular expressions in PHP…
• Quantifiers
• The frequency or position of bracketed
character sequences and single characters can
be denoted by a special character.
• Each special character having a specific
connotation. The +, *, ?, {int. range}, and $ flags
all follow a character sequence
9
1. Regular expressions in PHP…
• p+ : It matches any string containing at least one p.
• p* : It matches any string containing zero or more p's.
• p? : It matches any string containing zero or more p's. This is just
an alternative way to use p*.
• p{N}: It matches any string containing a sequence of N p's
• p{2,3}: It matches any string containing a sequence of two or
three p's.
• p{2, }: It matches any string containing a sequence of at least two
p's.
• p$: It matches any string with p at the end of it.
• ^p : It matches any string with p at the beginning of it.
10
1. Regular expressions in PHP…
• Start and end of the RE:
– optional, ||
• Sub-patterns:
– range of allowed characters
• [0-9]
– Allowed length
• {2}
• Sub-patterns with fixed character
11
1. Regular expressions in PHP…
• Matching from the start: ^:
– 1212-12-2014
Pattern exists if do not
match from start
12
1.1 Notations for RE
• ^:
– Match strings that start with the given pattern
• $:
– Match strings that end with the given pattern
• -:
– Range of characters
• [ ]:
– Makes a class of characters
• [^ ]:
– Negates the class of character
13
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
14
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, }
15
1.1 Notation for RE…
Predefined character ranges:
• \d:
– Exactly as [0-9]
• \D:
– Exactly as [^0-9]
• \w:
– Exactly as [a-zA-Z0-9]
16
1.1 Notation for RE…
RE examples:
– Validating date:
• |^\d{2}-\d{2}-\d{4}$|
– Validating CNIC:
• |^\d{5}-\d{7}-\d{1}$|
– Validating Email:
• |^[a-zA-Z0-9_.]+@[a-z]{3,5}.[a-z]{2,3}$|
17
1.1 Notation for RE…
– Validating name:
• |^[a-zA-Z ]{5,25}$|
– Validating Password:
• must contain ‘@’
– |@|
18
2. Validating User’s Input
• preg_match():
– searches a string for a specific pattern
– returns TRUE if it exists
– retruns FALSE otherwise
– preg_match(“pattern”,$string);
19
2. Validating User’s Input
• preg_match_all():
• The preg_match_all() function matches all
occurrences of pattern in string.
• preg_grep():
• The preg_grep() function searches all elements
of input_array, returning all elements matching
the regexp pattern.
20
2. Validating User’s Input…
21
2. Validating User’s Input…
Post To UserValidation.php
22
2. Validating User’s Input
Receiving Values
23
2. Validating User’s Input
Validating Name
24
2. Validating User’s Input
Validating CNIC
Validating DoB
25
2. Validating User’s Input
26
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 than second string
– -1 if second string is greater than first string
– strcmp($string1, $string2);
• strcasecmp():
– Compares two strings in case insensitive manner
– strcasecmp($string1, $string2);
27
3. String Functions in PHP…
28
3. String Functions in PHP…
Post to ValidatePass.php
29
3. String Functions in PHP…
Getting Variables
Using strlen()
30
3. String Functions in PHP…
31
3. String Functions in PHP…
33
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);
34
3. String functions in PHP…
35
3. String functions in PHP…
Converts to Lowercase
Converts to Uppercase
Using ucfirst()
Using ucwords()
36
3. String functions in PHP…
37
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);
38
3. String functions in PHP…
First Occurrence of ‘E’
39
3. String functions in PHP…
40
Summary of PHP Lectures
• Setting the environment
• PHP overview
– What is a PHP File
– Open-source
– Platform independent
– What Can PHP Do?
– Why PHP?
– Basic PHP Syntax
– Writing and Executing PHP Code
• PHP constants
– Constants are Global
• PHP variables
– Local
– Global
– Static
– Type Determination
• PHP Strings
• PHP is a Loosely Typed Language 41
Summary of PHP Lectures
• Operators in PHP
• Arithmetic Operators: +, - ,*, /, %, **
• Assignment Operators: =
• String Operators: . , .=
• Increment/decrement Operators: ++ , --
• Logical Operators: AND, OR, NOT, XOR, &&, ||, !
• Comparison Operators: >, <, <=, >=
• Equality Operators: ==, !=, ===
• Conditional statements
• if statement - executes some code if one condition is true
• if...else statement - executes some code if a condition is true and another code if
that condition is false
• if...elseif....else statement - executes different codes for more than two conditions
• switch statement - selects one of many blocks of code to be executed
42
Summary of PHP Lectures
• Looping statements
• For Loop
• While Loop
• Do-While Loop
• ForEach Loop
• Arrays in PHP
• Associative arrays
• Sorting arrays
43
Summary of PHP Lectures
• Passing Form Data
– action
– method (POST or GET)
• When to Use GET?
• When to Use POST?
• Compare GET vs. POST
• Super Global Variables
44
Summary of PHP Lectures
• Passing data with forms
– Passing Text Field Data
– Passing Hidden Field Data
– Getting Value From Checkbox
– Getting Value From Radio Button
– Getting Value From Select List
• Using session Variables
45
Summary of Today’s Lecture
• Writing regular expression in PHP
• Brackets []
• Quantifiers +, *, ?, {int. range}, and $
• Sub-patterns
• Predefined character ranges\d:\D:\w:
46
Summary of Today’s Lecture
• Defined functions.
• preg_match():
• preg_match_all():
• preg_grep():
48