HTML and CSS Reference
EXPLANATION ( CONTINUED )
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.
If the user entered only alphabetic characters in his or her name, true will be re-
turned, and the form will be submitted.
This is where the HTML form starts.
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
The user enters his or her name in the text field called fname .
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.
<head><title>Removing Spaces and Dashes</title></head>
<h2>Removing Spaces and Hyphens</h2>
var string="444- 33 - 12 34"
var regex = /[ -]+/g;
var newString= string.replace(regex, "");
document.write("The original string: "+string);