Form Validation Check
Form Validation Check
Form Validation Check
CS380
What is form validation?
2
CS380
Client vs. server-side validation
4
CS380
Basic server-side validation
6
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
}
?> PHP
CS380
Basic server-side validation
7
code
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?
CS380
Regular expressions
8
/http:\/\
// #http://# #better readability
PHPRegExp
Used for Perl regular expressions (preg)
CS380
Basic Regular Expression
10
/abc/
CS380
Special characters: |, (), ^, \
12
| means OR
"/abc|def|g/" matches "abc", "def", or "g"
There's no AND symbol. Why not?
CS380
Special characters: |, (), ^, \
13
CS380
Quantifiers: *, +, ?
14
CS380
Character sets: []
16
CS380
Character ranges: [start-end]
18
CS380
Escape sequences
19
$matchesarray[0] = "http://www.tipsntutorials.com/"
$matchesarray[1] = "http://"
$matchesarray[2] = "www.tipsntutorials.com/"
preg_match ('/(http://)(.*)/', "http://www.tipsntuto
rials.com/", $matchesarray)
PHP
CS380
Regular expressions example
22
CS380
PHP form validation w/ regexes
23
$state = $_REQUEST["state"];
if (!preg_match("/[A-Z]{2}/", $state)) {
?>
<h2>Error, invalid state submitted.</h2>
<?php
}
PHP
CS380
Another PHP experiment
24
CS380