Java Reference
In-Depth Information
}
return formValid;
}
</script>
<form name=”form1” onsubmit=”return checkForm(document.form1)” action=””>
<input type=”text” id=”text1” name=”text1” />
<br />
CheckBox 1<input type=”checkbox” id=”checkbox2” name=”checkbox2” />
<br />
CheckBox 1<input type=”checkbox” id=”checkbox1” name=”checkbox1” />
<br />
<input type=”text” id=”text2” name=”text2” />
<p>
<input type=”submit” value=”Submit” id=”submit1” name=”submit1” />
</p>
</form>
</body>
</html>
Exercise 2 Solution
The bug-free version looks like this:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 4, Question 2: Answer</title>
</head>
<body>
<script type=”text/javascript”>
function checkForm(theForm)
{
var formValid = true;
var elementCount = 0;
while(elementCount < theForm.length)
{
if (theForm.elements[elementCount].type == “text”)
{
if (theForm.elements[elementCount].value == “”)
{
alert(“Please complete all form elements”)
theForm.elements[elementCount].focus();
formValid = false;
break;
}
}
elementCount++;
}
return formValid;
Search WWH ::




Custom Search