Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. // Add the novalidate attribute when the JS loads
  2. var forms = document.querySelectorAll('.validate');
  3. for (var i = 0; i < forms.length; i++) {
  4. forms[i].setAttribute('novalidate', true);
  5. }
  6.  
  7.  
  8. // Validate the field
  9. var hasError = function (field) {
  10.  
  11. // Don't validate submits, buttons, file and reset inputs, and disabled fields
  12. if (field.disabled || field.type === 'file' || field.type === 'reset' || field.type === 'submit' || field.type === 'button') return;
  13.  
  14. // Get validity
  15. var validity = field.validity;
  16.  
  17. // If valid, return null
  18. if (validity.valid) return;
  19.  
  20. // If field is required and empty
  21. if (validity.valueMissing) return 'Bitte diese Felder einfuellen';
  22.  
  23. // If not the right type
  24. if (validity.typeMismatch) {
  25.  
  26. // Email
  27. if (field.type === 'email') return 'Bitte richtige email adresse eintragen';
  28. }
  29. //
  30. if (validity.valueMissing != null) return ;
  31.  
  32. // If number input isn't a number
  33. if (validity.badInput) return 'Bitte Nummer eintragen.';
  34.  
  35. // If a number value doesn't match the step interval
  36. if (validity.stepMismatch) return 'Nummer muss in richtige form sein';
  37.  
  38. // If pattern doesn't match
  39. if (validity.patternMismatch) {
  40.  
  41. // If pattern info is included, return custom error
  42. if (field.hasAttribute('title')) return field.getAttribute('title');
  43.  
  44. // Otherwise, generic error
  45. return 'Bitte daten in richtigen format schreiben';
  46.  
  47. }
  48.  
  49.  
  50. // If all else fails, return a generic catchall error
  51. return 'Wert für diese feld ist ungültig';
  52.  
  53. };
  54.  
  55.  
  56. // Show an error message
  57. var showError = function (field, error) {
  58.  
  59. // Add error class to field
  60. field.classList.add('error');
  61.  
  62. // If the field is a radio button and part of a group, error all and get the last item in the group
  63. if (field.type === 'radio' && field.name) {
  64. var group = document.getElementsByName(field.name);
  65. if (group.length > 0) {
  66. for (var i = 0; i < group.length; i++) {
  67. // Only check fields in current form
  68. if (group[i].form !== field.form) continue;
  69. group[i].classList.add('error');
  70. }
  71. field = group[group.length - 1];
  72. }
  73. }
  74.  
  75. // Get field id or name
  76. var id = field.id || field.name;
  77. if (!id) return;
  78.  
  79. // Check if error message field already exists
  80. // If not, create one
  81. var message = field.form.querySelector('.error-message#error-for-' + id );
  82. if (!message) {
  83. message = document.createElement('div');
  84. message.className = 'error-message';
  85. message.id = 'error-for-' + id;
  86.  
  87. // If the field is a radio button or checkbox, insert error after the label
  88. var label;
  89. if (field.type === 'radio' || field.type ==='checkbox') {
  90. label = field.form.querySelector('label[for="' + id + '"]') || field.parentNode;
  91. if (label) {
  92. label.parentNode.insertBefore( message, label.nextSibling );
  93. }
  94. }
  95.  
  96. // Otherwise, insert it after the field
  97. if (!label) {
  98. field.parentNode.insertBefore( message, field.nextSibling );
  99. }
  100.  
  101. }
  102. ////////////////////////////////
  103.  
  104.  
  105. // Add ARIA role to the field
  106. field.setAttribute('aria-describedby', 'error-for-' + id);
  107.  
  108. // Update error message
  109. message.innerHTML = error;
  110.  
  111. // Show error message
  112. message.style.display = 'inline-block';
  113. message.style.visibility = 'visible';
  114. message.style.color = 'red';
  115. message.style.fontSize = "xx-small";
  116.  
  117. };
  118.  
  119.  
  120. // Remove the error message
  121. var removeError = function (field) {
  122.  
  123. // Remove error class to field
  124. field.classList.remove('error');
  125.  
  126. // Remove ARIA role from the field
  127. field.removeAttribute('aria-describedby');
  128.  
  129. // If the field is a radio button and part of a group, remove error from all and get the last item in the group
  130. if (field.type === 'radio' && field.name) {
  131. var group = document.getElementsByName(field.name);
  132. if (group.length > 0) {
  133. for (var i = 0; i < group.length; i++) {
  134. // Only check fields in current form
  135. if (group[i].form !== field.form) continue;
  136. group[i].classList.remove('error');
  137. }
  138. field = group[group.length - 1];
  139. }
  140. }
  141.  
  142. // Get field id or name
  143. var id = field.id || field.name;
  144. if (!id) return;
  145.  
  146.  
  147. // Check if an error message is in the DOM
  148. var message = field.form.querySelector('.error-message#error-for-' + id + '');
  149. if (!message) return;
  150.  
  151. // If so, hide it
  152. message.innerHTML = '';
  153. message.style.display = 'none';
  154. message.style.visibility = 'hidden';
  155.  
  156. };
  157.  
  158.  
  159. // Listen to all blur events
  160. document.addEventListener('blur', function (event) {
  161.  
  162. // Only run if the field is in a form to be validated
  163. if (!event.target.form.classList.contains('validate')) return;
  164.  
  165. // Validate the field
  166. var error = hasError(event.target);
  167.  
  168. // If there's an error, show it
  169. if (error) {
  170. showError(event.target, error);
  171. return;
  172. }
  173.  
  174. // Otherwise, remove any existing error message
  175. removeError(event.target);
  176.  
  177. }, true);
  178.  
  179.  
  180. // Check all fields on submit
  181. document.addEventListener('submit', function (event) {
  182.  
  183. // Only run on forms flagged for validation
  184. if (!event.target.classList.contains('validate')) return;
  185.  
  186. // Get all of the form elements
  187. var fields = event.target.elements;
  188.  
  189. // Validate each field
  190. // Store the first field with an error to a variable so we can bring it into focus later
  191. var error, hasErrors;
  192. for (var i = 0; i < fields.length; i++) {
  193. error = hasError(fields[i]);
  194. if (error) {
  195. showError(fields[i], error);
  196. if (!hasErrors) {
  197. hasErrors = fields[i];
  198. }
  199. }
  200. }
  201.  
  202. // If there are errrors, don't submit form and focus on first element with error
  203. if (hasErrors) {
  204. event.preventDefault();
  205. hasErrors.focus();
  206. }
  207.  
  208. // Otherwise, let the form submit normally
  209. // You could also bolt in an Ajax form submit process here
  210.  
  211. }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement