Last active
February 13, 2024 22:55
-
-
Save ogabrielguerra/c2d6623e27735b1cdae392b875f158d4 to your computer and use it in GitHub Desktop.
[ReactJS] Form validation function
This file contains 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 validateForm = (formFields, formState) => { | |
let errors = []; | |
let allowedToProceed = true; | |
//All required fields are empty | |
if (!formState) { | |
for (let field of formFields) { | |
errors.push(field); | |
} | |
setFormErrors(errors); | |
return false; | |
} | |
for (let field of formFields) { | |
//Error found. Block. | |
if (!formState[field]) { | |
errors.push(field); | |
allowedToProceed = false; | |
} | |
} | |
//Update errors | |
setFormErrors(errors); | |
return allowedToProceed; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment