Invoking javascript from your page: ... For forms:
...
AllValidates() should call: validateFirstName() validateShoeSize(); etc AND the input for the firstName will have ... AND the input for the shoeSize will have ... This way we won't repeat code, and yet we call it both onblur of individual items, and onsubmit of the entire form. Also (good structure): the individual 'validate' functions will do two things: - determine correctness and return a boolean-ish value - AND, if it didn't validate, it will put an error message up to the user. [Presumably by adding a new node to the DOM, as in prev. lect.] Note that 'create/display an error message for shoeSize' and 'create/display an error message for firstName' etc shouldn't repeate code; that's why the hw also asks for a common helper-function 'displayErrorMessage' -- it'll be passed a string, and might be called by validateShoeSize and by validateFirstName etc. validateShoeSize - displayErrorMessage / validateAll - validateCarModel - displayErrorMessage \ validateFirstName - displayErrorMessage For displayErrorMessage -- WHERE in the DOM should it add the message? You'd like the error message to be located near the 'input' node of the DOM. My sol'n: pass displayErrorMessage *two* things -- both the string to display, *and* the DOM-node that failed-to-validate. Then, displayErrorMessage can locate that node, find its parent, and insert a new-textnode into the DOM right next to the offending node. E.g. function validateShoeSize(theShoeSizeInutNode) { if (theShoeSizeInutNode.value >= 200) { displayErrorMessage("your feet are HUGE", theShoeSizeInutNode); return false; } else { return true; } } Then, when calling it, pass in the node: ... Q: What's wrong with: function validateAll() { return validateShoeSize(...) && validateFirstName(...) && validateCarModel(...); } A: because of short-circuiting, if there are multiple errors you might not cause all of them to be displayed. Soln: function validateAll() { var isValid = true; isValid = isValid && validateShoeSize(...); isValid = isValid && validateFirstName(...); return isValid; } (gee, I wish I could write `isValid &&= validateShoeSize()`, but `&&=` isn't an operator even though `+=` is.)