main.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. vaeringjar copyright 2017
  3. Licenced as CC0
  4. BadFormPeter has basis on three basic ideas:
  5. * tag: onload this can tag the form with whatever asterisks for required fields
  6. * check: determines which required fields have a valid input
  7. and paints around the missing required labels
  8. and lists errors
  9. * submit: the success/valid case for check
  10. __
  11. ",'.
  12. ",\
  13. / Y BAD FORM PETER, BAD FORM!
  14. , _,'--.\
  15. \_-( ;--(/\ )
  16. \'. )6,6 /)
  17. \ \_ \ _, |_
  18. ', '--/'---'- /_/'-._
  19. '-'-L._ -|/ /-._ /
  20. | |-~'--._'-._/(,
  21. ,_/> / ''--~
  22. /\<_ _/
  23. // "-'-'\
  24. ,~-. ._ )
  25. / \_/ '\
  26. / / \ |
  27. / /` _\ |
  28. ( ,/ _.-' __.'
  29. / / [((---'
  30. / / ) )
  31. / / \/
  32. K=/
  33. snd / _>
  34. )_/
  35. */
  36. var BadFormPeter = function () {
  37. //this.foo = bar;
  38. };
  39. BadFormPeter.prototype = (function () {
  40. function _paintForm(id, cssName) {
  41. console.log("unimplemented");
  42. }
  43. function _checkform(formId) {
  44. var form = document.getElementById(formId);
  45. var missing = new Array();
  46. for(var current = 0; current < form.elements.length ; ++current) {
  47. var element = form.elements[current];
  48. if (element.hasAttribute("required")) {
  49. var cssName = "foo";
  50. var id = element.getAttribute("id");
  51. var name = element.getAttribute("name");
  52. var elementType = element.nodeName.toLowerCase();
  53. var prompt = "Required field " + current + ": " + elementType + " " + name;
  54. //TODO: Paint all the missing required elements' labels.
  55. if (elementType === "select") {
  56. //fail when option value empty
  57. console.log(prompt);
  58. } else if (elementType === "textarea") {
  59. //fail when empty
  60. console.log(prompt);
  61. } else if (elementType === "input") {
  62. var inputType = element.getAttribute("type");
  63. console.log(prompt + " with type " + inputType);
  64. if (inputType === "email") {
  65. //fail when invalid
  66. }else if (inputType === "date") {
  67. //fail when invalid
  68. } else if (inputType === "radio" || inputType === "checkbox") {
  69. //fail when none checked
  70. } else {
  71. // if (inputType === "text") {
  72. //fail when empty
  73. if (element.value === ""){
  74. missing.push(prompt);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. return missing;
  81. }
  82. function findLableForControl(el) {
  83. var idVal = el.id;
  84. labels = document.getElementsByTagName('label');
  85. for( var i = 0; i < labels.length; i++ ) {
  86. if (labels[i].htmlFor == idVal)
  87. return labels[i];
  88. }
  89. }
  90. return {
  91. onload: function (formId, buttonId, promptsId) {
  92. //TODO: Tag all the required elements' labels.
  93. var button = document.getElementById(buttonId);
  94. var label = findLableForControl(button);
  95. label.style.display = "none";
  96. button.addEventListener("click", function(event) {
  97. event.preventDefault();
  98. var missing = _checkform(formId);
  99. var prompts = document.getElementById(promptsId);
  100. prompts.parentElement.style.display = "none";
  101. prompts.innerHTML = "";
  102. if (missing !== undefined && missing.length > 0) {
  103. var errors = "<li>" + missing.join("</li><li>") + "</li>";
  104. prompts.parentElement.style.display = "block";
  105. prompts.innerHTML = errors;
  106. return false;
  107. } else {
  108. alert("blocked success");
  109. return false;
  110. }
  111. });
  112. }
  113. };
  114. })();