HTML and CSS Reference
In-Depth Information
if (!selected) {
alert(“You must choose a gender.”);
return false;
}
15
The form indicates that the Operating System field is also required, but because it's not
possible to not select an item in that select list, there's no need to validate it.
The Completed Registration Form with JavaScript Validation When the
JavaScript script that you just created is integrated with the original registration form
document from Lesson 11, the result is a web form that tests its contents before they're
transmitted to the server for further processing. This way, no data is sent to the server
until everything is correct. If a problem occurs, the browser informs the user (see
Figure 15.2).
FIGURE 15.2
An alert message.
So that you don't need to skip back to the exercise in Lesson 11 to obtain the Hypertext
Markup Language (HTML) source used when creating the form, here's the completed
form with the full JavaScript code.
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
<script language=”JavaScript”>
function checkform(thisform) {
if (!thisform.name.value) {
alert(“You must enter a name.”);
return false;
}
var selected = false;
for (var i = 0; i < thisform.gender.length; i++) {
if (thisform.gender[i].checked) {
selected = true;
}
}
 
Search WWH ::




Custom Search