Permalink
Browse files

Handle multipe forms on one page

  • Loading branch information...
redaxmedia committed Dec 16, 2018
1 parent c4abad6 commit de9e2d6044ba904565d8dee9e26295bbc9b3ac26
Showing with 25 additions and 23 deletions.
  1. +25 −23 modules/FormValidator/assets/scripts/form-validator.js
@@ -5,39 +5,41 @@ rs.modules.FormValidator.process = optionArray =>
...rs.modules.FormValidator.optionArray,
...optionArray
};
const form = document.querySelector(OPTION.selector);
const formList = document.querySelectorAll(OPTION.selector);
const validateArray =
[
'input',
'validate'
];

if (form)
if (formList)
{
/* handle validate */

form.querySelectorAll(OPTION.element.required).forEach(field =>
formList.forEach(form =>
{
const validateArray =
[
'input',
'validate'
];
/* handle validate */

field.classList.add(OPTION.className.fieldNote);
validateArray.forEach(validateEvent =>
form.querySelectorAll(OPTION.element.required).forEach(field =>
{
field.addEventListener(validateEvent, event =>
field.classList.add(OPTION.className.fieldNote);
validateArray.forEach(validateEvent =>
{
event.target.validity.valid ? event.target.classList.remove(OPTION.className.isError) : event.target.classList.add(OPTION.className.isError);
field.addEventListener(validateEvent, event =>
{
event.target.validity.valid ? event.target.classList.remove(OPTION.className.isError) : event.target.classList.add(OPTION.className.isError);
});
});
field.addEventListener('invalid', event =>
{
event.target.classList.add(OPTION.className.isError);
});
});
field.addEventListener('invalid', event =>
{
event.target.classList.add(OPTION.className.isError);
});
});

/* handle reset */
/* handle reset */

form.addEventListener('reset', () =>
{
form.querySelectorAll(OPTION.element.required).forEach(field => field.classList.remove(OPTION.className.isError));
form.addEventListener('reset', () =>
{
form.querySelectorAll(OPTION.element.required).forEach(field => field.classList.remove(OPTION.className.isError));
});
});
}
};

0 comments on commit de9e2d6

Please sign in to comment.