Chapter 16 Answers
You can send a form for validation prior to submitting it by adding the JavaScript
onSubmit
method to the<form>
tag. Make sure that your function returnstrue
if the form is to be submitted andfalse
otherwise.To match a string against a regular expression in JavaScript, use the
test
method.Regular expressions to match characters not in a word could be any of
/[^\w]/
,/[\W]/
,/[^a-zA-Z0-9_]/
, and so on.A regular expression to match either of the words âfoxâ or âfixâ could be
/f[oi]x/
.A regular expression to match any single word followed by any nonword character could be
/\w+\W/g
.A JavaScript function using regular expressions to test whether the word âfoxâ exists in the string âThe quick brown foxâ could be:
document.write(/fox/.test("The quick brown fox"))
A PHP function using a regular expression to replace all occurrences of the word âtheâ in âThe cow jumps over the moonâ with the word âmyâ could be:
$s=preg_replace("/the/i", "my", "The cow jumps over the moon");
The HTML keyword used to precomplete form fields with a value is the
value
keyword, which is placed within an<input>
tag and takes the formvalue="
value
"
.
Get Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.