|
home—lects—hws
D2L—breeze (snow day)
Due: 2016.Oct.28 (Fri) 11:00. Submit on D2L, and as usual have your a link to your form available at https://php.radford.edu/~yourUserId/itec325/hw05/index.php. (No hardcopy needed.)
We will further improve on server-side-validation—Server-side validation's Okaymon page by
adding client-side validation.
Make a copy of your directory from hw04, named hw05/.
(cp -pR hw04/ hw05 will
To report client-side verification errors:
If you have a field (say, “
Then, you will have a javascript function
(Extra credit:)
Check if a node
Remember:In HTML you specify a CSS-class using the attribute “class ”, but in javascript/DOM that's a reserved word and you instead want to set the DOM attribute “className ”.)
If you don't want to do this extra credit,
then instead include
var somePara = document.getElementById("my-favorite-para"); somePara.style.color = "blue"; somePara.innerHTML += "<em>watch out</em> — we're at the end."; |
(30pts)
For each input field, have a javascript function which validates it:
(a) returns true if the field is validated, and
(b) otherwise returns false and displays an error message
by calling
Use the same validation criteria as we did in hw04.
Note: Unfortunately, Javascript regexps do not yet support the unicode properties. So you can use/[A-Za-z]/ to detect letters.
It is not strictly necessary that your javascript
verify (say) that the drop-down contains one of the menu items,
since your HTML input already enforces that.
Likewise, it's not strictly necessary for the validation-function to
verify that the maximum-lengths are enforced, if your HTML
However, one advantage to still include these checks: If we have to repeat the validation code in two different places (in two different languages), then it's nice if those two copies of the code can be as similar as possible, more of a rote translation than requiring thought about what parts of the code can be elided from one version but are essential for the other.
Note: If the max-length is included in both the form and the php-validation, then you only want to mention it once! Add the max-lengths to your php-constants file, and have the form's php print the constants when generating (printing) the html-form.
Extra credit (5pts): Unit tests are not required for your javascript functions. However, if you are interested in doing some unit test for your javascript, see www.smashingmagazine.com/2012/06/27/introduction-to-javascript-unit-testing/.
If your form does not validate, it should not submit.
Make your page look neat and professional!
1You can also use the hw04-soln (once posted); just cite your source as usual. ↩
2
Note that setting
3
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 ); } |
4In 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. ↩
5the function to show-source a file is one of the few exceptions ↩
home—lects—hws
D2L—breeze (snow day)
©2016, Ian Barland, Radford University Last modified 2016.Oct.21 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |