Last active
January 4, 2017 06:42
-
-
Save eddiemoore/00f56137dfd705269670b1da68a721dd to your computer and use it in GitHub Desktop.
Simple function to check if all required fields are filled out correctly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isFormValid = form => { | |
const elements = form && form.elements | |
if (!elements) return false | |
return Array.from(elements).every(elem => elem.validity && elem.validity.valid) | |
} |
@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
@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
@eddiemoore Is this a ponyfill for
HTMLFormElement.prototype.checkValidity()
?Not sure why this should return
false
if the form does not have a validelements
property. Maybe throw an error?