|
home—lects—exams—hws
D2L—breeze (snow day)
Due: 2012.Mar.26 (Tue) 23:59. (Accepted through Wed. 23:59)
However, try to have it done by Friday, or at least all conceptual hurdles cleared,
so you can use the weekend for hw06: server-side validation.
We will further improve on hw04—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 hw04/ 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-Anne Josephine Montgomery-Richardson 1 won't be offended, but not too much 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 charactes, 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.).
As part of this step, you must have a javascript function that takes in a string and two integers (an upper and lower bound), and returns an error message (if it doesn't represent an integer in that range, inclusive), or the empty string (if it does). Be sure to give a descriptive name to this function.
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 lect06a-js—case study: refactoring javascript; 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 stickiness.)
Since these two options are mutually-exclusive, when a user selects one
then
the other checkbox
should be disabled789.
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!
1She probably goes by “Kimmy-Jo M.R.”, but still… ↩
2 for extra credit: you can rectify and update the user's input, as long as you notify them that you made the change. ↩
3For 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. ↩
4For extra credit, only allow the credit card number if the seperators are all "-", or all " ". ↩
5 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. ↩
6
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 ); } |
7
You disable an input field by
setting its attribute
8 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 js/lect06a-js.html. ↩
9 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. ↩
10 And when you read/write “disabled”, don't confused that with “checked” like I did, which cost me a couple hours of debugging. ↩
11the function to show-source a file is one of the few exceptions ↩
home—lects—exams—hws
D2L—breeze (snow day)
©2012, Ian Barland, Radford University Last modified 2013.Mar.26 (Tue) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |