123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /*
- vaeringjar copyright 2017
- Licenced as CC0
- BadFormPeter has basis on three basic ideas:
- * tag: onload this can tag the form with whatever asterisks for required fields
- * check: determines which required fields have a valid input
- and paints around the missing required labels
- and lists errors
- * submit: the success/valid case for check
- __
- ",'.
- ",\
- / Y BAD FORM PETER, BAD FORM!
- , _,'--.\
- \_-( ;--(/\ )
- \'. )6,6 /)
- \ \_ \ _, |_
- ', '--/'---'- /_/'-._
- '-'-L._ -|/ /-._ /
- | |-~'--._'-._/(,
- ,_/> / ''--~
- /\<_ _/
- // "-'-'\
- ,~-. ._ )
- / \_/ '\
- / / \ |
- / /` _\ |
- ( ,/ _.-' __.'
- / / [((---'
- / / ) )
- / / \/
- K=/
- snd / _>
- )_/
- */
- var BadFormPeter = function () {
- //this.foo = bar;
- };
- BadFormPeter.prototype = (function () {
-
- function _paintForm(id, cssName) {
- console.log("unimplemented");
- }
- function _checkform(formId) {
-
- var form = document.getElementById(formId);
-
- var missing = new Array();
- for(var current = 0; current < form.elements.length ; ++current) {
-
- var element = form.elements[current];
-
- if (element.hasAttribute("required")) {
-
- var cssName = "foo";
- var id = element.getAttribute("id");
- var name = element.getAttribute("name");
- var elementType = element.nodeName.toLowerCase();
- var prompt = "Required field " + current + ": " + elementType + " " + name;
-
-
- //TODO: Paint all the missing required elements' labels.
- if (elementType === "select") {
- //fail when option value empty
- console.log(prompt);
- } else if (elementType === "textarea") {
- //fail when empty
- console.log(prompt);
- } else if (elementType === "input") {
- var inputType = element.getAttribute("type");
- console.log(prompt + " with type " + inputType);
- if (inputType === "email") {
- //fail when invalid
- }else if (inputType === "date") {
- //fail when invalid
- } else if (inputType === "radio" || inputType === "checkbox") {
- //fail when none checked
- } else {
- // if (inputType === "text") {
- //fail when empty
- if (element.value === ""){
- missing.push(prompt);
- }
-
- }
- }
-
- }
- }
- return missing;
- }
- function findLableForControl(el) {
- var idVal = el.id;
- labels = document.getElementsByTagName('label');
- for( var i = 0; i < labels.length; i++ ) {
- if (labels[i].htmlFor == idVal)
- return labels[i];
- }
- }
-
- return {
- onload: function (formId, buttonId, promptsId) {
-
- //TODO: Tag all the required elements' labels.
-
- var button = document.getElementById(buttonId);
-
- var label = findLableForControl(button);
- label.style.display = "none";
-
- button.addEventListener("click", function(event) {
- event.preventDefault();
- var missing = _checkform(formId);
- var prompts = document.getElementById(promptsId);
- prompts.parentElement.style.display = "none";
- prompts.innerHTML = "";
- if (missing !== undefined && missing.length > 0) {
- var errors = "<li>" + missing.join("</li><li>") + "</li>";
- prompts.parentElement.style.display = "block";
- prompts.innerHTML = errors;
- return false;
- } else {
- alert("blocked success");
- return false;
- }
- });
- }
- };
- })();
|