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