|
home—lects—hws
D2L—breeze (snow day)
Due: 2014.Mar.26 (Wed)
28 (Fri) 14:00.
14:00 23:59
We will further improve on hw03—sanitized html: and automated source-listings's .W.o.W. page by
adding client-side validation.
Make a copy of your directory from hw04, named hw05/.
(cp -pR hw03/ hw05 will
Include the following input-validation, client-side (through attributes on html input tags, and/or javascript).
“Your name”: required. Make it long enough that somebody named Kimberleigh-Annabelle Josephine Montgomery-Richardson 2 won't be offended …but not ludicrously longer than that, to protect against somebody who inadvertently pastes in some other long text into that input.
In the spirit of user-friendliness, we won't disallow any particular characters, but we will require at least one “letter” character: A through Z, upper or lower case. (For slight extra-credit (2pts), instead require at least one unicode letter, so that you can get accented letters, etc.).
Strive to make the error-messages as specific as possible. For example, if the user included an illegal character in a textbox, then the error message:
If the input field fails validation, dynamically modify the html page to insert (or remove/zero-out) an error message. (Recall lect-js-hiding-parts-v3a.html; Use css to control the style of the error message.)
To report client-side verification errors:
If you have a field (say, “
Then, you will have a javascript function
check if a node
var somePara = document.getElementById("my-favorite-para"); somePara.style.color = "blue"; somePara.innerHTML += "<em>watch out, we're at the end!</em>" |
Include two check-boxes: one for “do not actually submit (even if the form info all validates)”, and one for “submit form even if validation fails”. (These aren't usual end-user options; we include them just so that we can still test our client-side validation, and in the future server-side validation.) Your form should submit as you'd expect: If (and only if) submit-if-fails is checked, or (the form validates and do-not-submit is unchecked).
Since these two options are mutually-exclusive, when a user selects one
then
the other checkbox
should be disabled8910.
When one of those boxes becomes unchecked,
make sure the other checkbox is un-disabled.
(Hint: set the other checkbox's
Make your page look neat and professional!
1In this case, short-circuiting is not what we want. This stems from the fact that our validation-functions not just returning a value; they are having the side-effect of displaying the error. Many (including me) would argue it's poor practice to have a function which both returns and has a side-effect, but I'll make an exception for functions which are so intrinsically tied to I/O, the ultimate side-effect. ↩
2She probably goes by “Kimmy-Jo M.R.”, but still… ↩
3 for extra credit: you can rectify and update the user's input, as long as you notify them that you made the change. ↩
4For extra credit, verify the number using the en.wikipedia.org/wiki/Luhn_algorithm. Write the function yourself, w/o just cribbing it from somewhere else. Between the wikipedia discussion (and their (non-javascript) code) and a general javascript reference, you have all the information you need. ↩
5For extra credit, only allow the credit card number if the seperators are all "-", or all " ". ↩
6 Be aware that this allows for HTML injection. As long as somebody is just injecting HTML into their own page that's fine. We'll of course sanitize any input that the server receives, before printing out to somebody else's web page, or entering it into a database. ↩
7
There is, oddly, no standard js/DOM function “
/** Insert a node into the DOM after the referenceNode. * (Complements javascript's existing 'insertBefore'.) * @param (node) referenceNode -- the node after which the new node will be added. * @param (node) newNode -- the new DOM node to add. * @return void * @author lobo235, at http://www.netlobo.com/javascript-insertafter.html * @version 2007.Nov.07 */ function insertAfter( referenceNode, newNode ) { referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling ); } |
8
You disable an input field by
setting its attribute
9 We saw how to set the attribute of a DOM node using javascript; that is similar to how we toggled the “display” attribute of paragraphs, in lect-js—case study: refactoring javascript. ↩
10 Usually, radio-buttons are used for mutually exclusive options, though in this case you'd need three. However, for this hw you are required to use checkboxes that are dynamically disabled, to learn that skill. ↩
11 And when you read/write “disabled”, don't confused that with “checked” like I did, which cost me a couple hours of debugging. ↩
12the function to show-source a file is one of the few exceptions ↩
home—lects—hws
D2L—breeze (snow day)
©2014, Ian Barland, Radford University Last modified 2014.Jun.04 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |