Skip to content

Instantly share code, notes, and snippets.

@eddiemoore
Last active January 4, 2017 06:42
Show Gist options
  • Save eddiemoore/00f56137dfd705269670b1da68a721dd to your computer and use it in GitHub Desktop.
Save eddiemoore/00f56137dfd705269670b1da68a721dd to your computer and use it in GitHub Desktop.
Simple function to check if all required fields are filled out correctly
const isFormValid = form => {
const elements = form && form.elements
if (!elements) return false
return Array.from(elements).every(elem => elem.validity && elem.validity.valid)
}
@sebdeckers
Copy link

@eddiemoore Is this a ponyfill for HTMLFormElement.prototype.checkValidity()?

Not sure why this should return false if the form does not have a valid elements property. Maybe throw an error?

@eddiemoore
Copy link
Author

@sebdeckers returning false if the form doesn't exist basically. cause if it is a form it should have the .elements "array". even if there are no elements .elements should still return something. So if the form is invalid then the form is not valid

@sebdeckers
Copy link

sebdeckers commented Jan 4, 2017

@eddiemoore Throws when form is undefined but otherwise similar behaviour in the for/of style.

function isFormValid ({elements}) {
  for (const {validity} of Array.from(elements)) {
    if (validity && !validity.valid) return false
  }
  return true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment