HW6 is due Wednesday
CP8 is due today - consider opting in the CP8 showcase! Even if it's a neat dataset setup.sql file you've created :)
For CP8, remember to omit any Cloud 9 links - all links to your files should be relative. Also make sure your files are zipped correctly - some students have not been correctly zipping files from Cloud9. To check, try unzipping your .zip folder locally.
Cookies on Wednesday! (Real ones :))
Many websites offer features that allow users to interact with the page. Unfortunately, not all users will behave as expected.
Why is it important to spend the time prioritizing validation as web developers?
Which types of website features do you think could be most common for validating user input? (think of your own experience as users for various websites)
User input validation is the process of ensuring that any user input is well-formed and correct (valid).
What are some examples of validation on the web you can think of? Consider general user input use cases as well as specific websites of which you may have provided user input on.
Validation can be performed:
(Lab06 PHP Array Mystery) Consider the following PHP code:
function array_mystery($arr) {
for ($i = 1; $i < count($arr); $i++) {
$arr[$i] = $arr[$i] + $arr[$i - 1];
}
return $arr;
}
PHP
Indicate in the right-hand column what values would be stored in the returned array after the
function array_mystery
executes if the array in the left-hand column is passed as a parameter
to array_mystery
. Include your answers in the format of [a, b,
c]
where a,
b, and c are numbers in the array result (for a 3-element array).
[8]
:
[6, 3]
:
[1, 2, 3, 4]
:
[7, 10, 12, 12, 17]
:
HTML
output
Let's validate this form's data on the server...
$city = $_POST["city"];
$state = $_POST["state"];
$zip = $_POST["zip"];
if (!$city || strlen($state) != 2 || strlen($zip) != 5) {
print "Error, invalid city/state/zip submitted.";
}
PHP
Basic idea: Examine parameter values, and if they are bad, show an error message and abort. But there are some limitations given what we've learned so far in this course.
/^[a-zA-Z_\-]+@(([a-zA-Z_\-])+\.)+[a-zA-Z]{2,4}$/
Regular expression ("regex"): a description of a pattern of text
Regular expressions are extremely power but tough to read (the above regular expression matches email addresses)
Regular expressions occur in many places:
split
method (CSE 143
rando grammar generator)
As we review some of the different ways we can construct regular expressions, write two of your own "regular expression" problems. No need to write answers, but give it a shot! We may feature some creative ideas in section tomorrow :)
/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"
A . matches any character except a \n line break
/.ow.l./
matches "Mowgli", "Powell", etc.A trailing i at the end of a regex (after the closing /) signifies a case-insensitive match
/cal/i
matches "Pascal", "California", "GCal", etc.| means OR
/abc|def|g/
matches "abc", "def", or "g"() are for grouping
/iP(ad|hone)/
matches "iPad" or "iPhone"\ starts an escape sequence
/<br \/>/
matches lines containing <br /> tags* 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!?xyz__9a", ...+ means 1 or more occurrences
/Hi!+ there/
matches "Hi! there", "Hi!!! there!", .../a(bc)+/
matches "abc", "abcbc", "abcbcbc", ...? means 0 or 1 occurrences
/a(bc)?/
matches only "a" or "abc"{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
When you search Google, it shows the number of pages of results as the number of "o"s in the word "Google".
What regex matches such words with an even number of 'o's ("Google", "Goooogle", "Goooooogle", ...?
Your regex should not match strings with fewer than two o's and shold be case-sensitive (only the first letter should be capitalized) (try it)
Solution: G(oo)+gle
or
Go{2}+gle
both work!
^ represents the beginning of the string or line; $ represents the end
/Doggy/
matches all strings that contain Doggy/^Doggy/
matches all strings that start with Doggy/Doggy$/
matches all strings that end with Doggy/^Doggy$/
matches the exact string "Doggy" only/^Mo.*Doggy$/
matches "MoDoggy", "Mowgli Doggy", "Mowgli is my Doggy", ... but
not "Doggy Mowgli is my Doggy", "Mowgli" or "my Doggy"
(on the other slides, when we say, /PATTERN/ matches "text", we really mean that it matches any string that contains the text)
[] groups characters into a character set; will match any single character from the set
/[bcd]art/
matches strings containg "bart", "cart", and "dart"/(b|c|d)art/
but shorterInside [], many of the modifier keys act as normal characters
/what[!*?]*/
matches "what", "what!", "what?**!", "what??!", etc.
Practice: What regex matches strings containing a lowercase vowel? (try it!)
Practice: What regex matches strings containing consecutive vowels? (try it!)
Inside a character set, specify a range of characters with -
/[a-z]/
matches any lowercase letter/[a-zA-Z0-9]/
matches any lowercase or uppercase letter or digitAn initial ^ inside a character set negates it
/[^abcd]/
matches any character other than a, b, c, or dInside a character set, - must be escaped to be matched
/[+\-]?[0-9]+/
matches an optional + or -,
followed by at least one digit
Practice: What regular expression matches UW Student ID numbers? (non-negative 7 digit numbers) (try it!)
Practice: What regular expression matches camelCasing? (match only trings with at least one capital letter; only alphabetical characters are allowed) (try it!)
Practice: What regular expression matches a sequence of only consonants (non-vowel letters) assuming that the string consists only of lowercase letters? (try it!)
Special escape sequence characters sets
Practice: What regular expression matches any string that contains a tab (\t) character?
Practice: What regular expression matches names in a "Last, First M." format, with any number of spaces?
We will pass out reference sheets in section tomorrow!
Regex syntax: strings that begin and end with /, such as "/[AEIOU]+/"
function | description |
---|---|
preg_match(regex, string) | returns TRUE if string matches regex |
preg_replace (regex, replacement, string) | returns a new string with all substrings that match regex replaced by replacement |
preg_split (regex, string) | returns an array of strings from given string broken apart using given regex as delimiter (like explode but more powerful) |
$state = $_POST["state"];
if (!preg_match("/^[A-Z]{2}$/", $state)) {
print "Error, invalid state submitted.";
}
PHP
preg_match
and regexes help you to validate parameters
Websites often don't want to give a descriptive error message here (why?)
# 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")
PHP
HTML Form Validation (MDN): A neat overview of the different features offered in HTML5 for client-side form validation!
RegexOne: A helpful interactive regex tutorial
Regex Crossword Game: A super fun way to practice regex for puzzle-lovers :)