1. |
if (allGood && !isNum(thisTag. value)) classBack = "invalid ";
classBack += thisClass;
This goes into the isNum block of the switch/case conditional. If the entry is non-numeric, isNum() returns false.
|
2. |
if (allGood && !isZip(thisTag. value)) classBack = "invalid ";
This line has been added to the isZip switch/case block. If the field is not blank and it's not a Zip code, isZip() returns false.
|
3. |
function isNum(passedVal) {
First, define the function isNum. The variable passedVal was sent to the function from the form.
|
4. |
if (passedVal == "") {
return false;
}
If passedVal is empty, then the field we're looking at isn't a number. When that happens, return false, signaling an error.
|
5. |
for (var k=0; k<passedVal.length; k++) {
Now scan through the length of passedVal, incrementing the k counter each time it goes through the loop. We're using k because we're already inside another loop.
|
| |
6. |
if (passedVal.charAt(k) < "0") {
return false;
}
if (passedVal.charAt(k) > "9") {
return false;
}
The charAt() operator checks the character at the position k. If the character is less than "0" or greater than "9," it isn't a digit, so bail out and declare the input to be non-numeric, or false.
|
7. |
If we make it here, we've got a number, so we return true.
|
8. |
function isZip(inZip) {
if (inZip == "") {
return true;
}
return (isNum(inZip));
}
In the context of this form, it's valid for the Zip code field to be empty. Because of that, we first check the field to see if the user entered anything, and if they didn't, we return trueit's a valid entry. If they did enter anything, though, it needs to be numeric, so that's the next check.
|