Chapter 17 Answers

Question 17-1

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 returns true if the form is to be submitted and false otherwise.

Question 17-2

To match a string against a regular expression in JavaScript, use the test method.

Question 17-3

Regular expressions to match characters not in a word could be any of /[^\w]/, /[\W]/, /[^a-zA-Z0-9_]/, and so on.

Question 17-4

A regular expression to match either of the words fox or fix could be /f[oi]x/.

Question 17-5

A regular expression to match any single word followed by any non-word character could be /\w+\W/g.

Question 17-6

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"))
Question 17-7

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");
Question 17-8

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 form value="value".

Get Learning PHP, MySQL, and JavaScript 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.