Except where otherwise noted, the contents of this presentation are © Copyright 2008 Marty Stepp and Jessica Miller and are licensed under the Creative Commons Attribution 2.5 License.
Validation can be performed:
<form id="exampleform" action="http://foo.com/foo.php" method="get"> <fieldset> City: <input id="city" type="text" name="city" /> <br /> State: <input id="state" type="text" name="state" size="2" /> <br /> <input type="submit" /> </fieldset> </form>
$city = $_REQUEST["city"]; $state = $_REQUEST["state"]; if ($city == "" || strlen($state) != 2) { ?> <h2>Error, invalid city/state submitted.</h2> <?php }
<form id="exampleform" action="http://foo.com/foo.php" method="get">
window.onload = function() {
$("exampleform").onsubmit = checkData;
};
function checkData(event) {
if ($("city").value == "" || $("state").value.length != 2) {
Event.stop(event);
alert("Error, invalid city/state."); // show error message
}
}
onsubmit
and onreset
eventsEvent.stop
on the event/^[\w\.%\-]+@[\w.\-]+\.[a-zA-Z]{2,4}$/
Scanner
, String
's split
method (CSE 143 sentence generator)/abc/
/
"abc"
:
"abc"
,
"abcdef"
,
"defabc"
,
".=.abc.=."
,
...
"fedcba"
,
"ab c"
,
"JavaScript"
,
...
.
.
matches any character except a \n
line break
/.oo.y/
matches
"Doocy"
,
"goofy"
,
"PooPy"
,
...
i
at the end of a regex (after the closing /
) signifies a case-insensitive match
/mart/i
matches
"Marty Stepp"
,
"smart fellow"
,
"WALMART"
,
...
|
, ()
, ^
, \
|
means OR
/abc|def|g/
matches "abc"
, "def"
, or "g"
()
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
/ \ $ . [ ] ( ) ^ * + ?
/<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!?_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"
{min,max}
{min,max}
means between min and max occurrences (inclusive)
/a(bc){2,4}/
matches "abcbc"
, "abcbcbc"
, or "abcbcbcbc"
{2,}
means 2 or more{,6}
means up to 6{3}
means exactly 3[]
[]
group characters into a character set; /[bcd]art/
matches strings containing "bart"
, "cart"
, and "dart"
/(b|c|d)art/
but shorter[]
, many of the modifier keys act as normal characters
/what[!*?]*/
matches "what"
, "what!"
, "what?**!"
, "what??!"
, .../[ACGT]+/
[start-end]
-
/[a-z]/
matches any lowercase letter/[a-zA-Z0-9]/
matches any lower- or uppercase letter or digit^
inside a character set negates it
/[^abcd]/
matches any character other than a, b, c, or d-
must be escaped to be matched
/[+\-]?[0-9]+/
matches an optional +
or -
, followed by at least one digit/[ABCDF][+\-]?/
\d
matches any digit (same as [0-9]
);
\D
any non-digit ([^0-9]
)
\w
matches any word character(
[a-zA-Z_0-9]
);
\W
any non-word char
\s
matches any whitespace character ( , \t
, \n
, etc.);
\S
any non-whitespace
/^[\w\.%\-]+@[\w.\-]+\.[a-zA-Z]{2,4}$/
/\$\d{3,}\.\d{2}/
/
, such as "/[AEIOU]+/"
preg_match(regex, string)
TRUE
if string matches regex
i
at end of regular expression (after closing /
)preg_replace(regex, replacement, string)
preg_split(regex, string)
explode
but more powerful)# 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")
\
must be escaped to \\
$state = $_REQUEST["state"]; if (!preg_match("/[A-Z]{2}/", $state)) { ?> <h2>Error, invalid state submitted.</h2> <?php }
preg_match
and well-chosen regexes allows you to quickly validate query parameters against complex patternsstring.match(regex)
- if string fits the pattern, returns the matching text; else returns
null
- can be used as a Boolean truthy/falsey test:
var name = $("name").value;
if (name.match(/[a-z]+/)) { ... }
i
can be placed after the regex for a case-insensitive match
name.match(/Marty/i)
will match "marty"
, "MaRtY"
, ...string.replace(regex, "text")
- replaces the first occurrence of given pattern with the given text
var str = "Marty Stepp";
str.replace(/[a-z]/, "x")
returns "Mxrty Stepp"
- returns the modified string as its result; must be stored
str = str.replace(/[a-z]/, "x")
g
can be placed after the regex for a global match (replace all occurrences)
str.replace(/[a-z]/g, "x")
returns "Mxxxx Sxxxx"
str = str.replace(/[^A-Z]+/g, "")
turns str
into "MS"