Example 2: a zip code validator
Suppose we want to write a JavaScript function that takes a variable and returns whether or not the variable is a string representing a 5 digit US zip code ( and let us assume here that any 5 digit number is valid). Assume our function is called isValidZipCode(proposedZipCode) and, again, that it lives in myJsScripts.js. A suitable Test Page would look like the following:
<html>
<head>
<title>Test Page for isValidZipCode(proposedZipCode)</title>
<script language="javascript" src="jsUnitCore.js"></script>
<script language="javascript" src="myJsScripts.js"></script>
</head>
<body>
<script language="javascript">
function testValidZipCode() {
assert(isValidZipCode("90210"));
assert(isValidZipCode("94114"));
}
function testInvalidZipCode() {
assert(!isValidZipCode("foo"));
assert(!isValidZipCode("9021"));
}
</script>
</body>
</html>