HTML and CSS Reference
In-Depth Information
EXAMPLE
9.27 (
CONTINUED
)
2
while(
email_addr.indexOf("@") == -1
){
3
alert( "Invalid email address.");
email_addr=prompt("What is your email address? ","");
}
document.write("<br />OK.<br />");
</script>
</big>
</font>
</body>
</html>
EXPLANATION
1
The user is prompted for his or her e-mail address and the input is assigned to a
string called
email_addr
.
2
The loop expression uses the
indexOf() String
method to see if there is an
@
sym-
bol in the e-mail address. If there isn't, the
indexOf()
method returns -1 and the
body of the loop is executed.
3
If the
indexOf()
method didn't find the
@
substring, the alert box appears and the
user is prompted again (see Figures 9.31 and 9.32). The loop terminates when the
user enters an e-mail address containing an
@
sign. Of course, this is just a simple
test for validating an e-mail address; more elaborate methods of validation are dis-
cussed in Chapter 17, “Regular Expressions and Pattern Matching.”
Figure 9.31
Using the
indexOf() String
method.
Figure 9.32
The user entered an e-mail address without the
@
symbol.