Skip to content

Instantly share code, notes, and snippets.

@ogabrielguerra
Last active February 13, 2024 22:55
Show Gist options
  • Save ogabrielguerra/c2d6623e27735b1cdae392b875f158d4 to your computer and use it in GitHub Desktop.
Save ogabrielguerra/c2d6623e27735b1cdae392b875f158d4 to your computer and use it in GitHub Desktop.
[ReactJS] Form validation function
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