HTML and CSS Reference
In-Depth Information
EXPLANATION
(
CONTINUED
)
3
If the user didn't enter anything at all and the field is empty, another alert will be
sent to the user, and
false
will be returned. The form will not be submitted.
4
If the user entered only alphabetic characters in his or her name,
true
will be re-
turned, and the form will be submitted.
5
This is where the HTML form starts.
6
The
onSubmit
handler will be triggered when the user clicks the Submit button,
and the
okAlpha()
function will be called, passing a reference to the form called
alphaTest
.
7
The user enters his or her name in the text field called
fname
.
8
After filling out the form, the user will click the Submit button, thereby triggering
the
onSubmit
handler on line 6. See Figure 17.40.
Figure 17.40
The user has a digit in her name. She can only enter alphabetic
characters, or she will see the warning.
Removing Spaces and Dashes.
To remove any unwanted spaces or dashes from
user input, the
String
object's
replace()
method can be used to find the characters and
replace them with nothing, as shown in Example 17.39.
EXAMPLE
17.39
<html>
<head><title>Removing Spaces and Dashes</title></head>
<body bgcolor="magenta">
<big>
<h2>Removing Spaces and Hyphens</h2>
<script type= "text/javascript">
1
var string="444- 33 - 12 34"
2
var regex = /[ -]+/g;
3
var newString=
string.replace(regex, "");
document.write("The original string: "+string);
Continues