With this example, where I assume all of your inputs are controlled, you either need to backlink those values to the element, to know what kind of alert to throw and get some reasonable normal text to throw (you probably don't want to throw "You are missing input5") from data property of HTML element, or in your instance I'd just go and if(){} else if(){} every single case.
The better thing to do would be to get all the inputs from event that you are getting on handleSubmit and check it with those values.
As it's not exactly straight forward to have good responding form in multiple languages there are packages available to solve this. I'd recommend https://www.npmjs.com/package/yup.
Also checking for empty input by doing !value is not a good practice, as when you get to TypeScript you might encounter the fact that 0 == True inside of your numeric inputs and EsLint errors regarding not explicitly asking to handle the nullish/empty cases explicitly.
Here is a one way of doing it:
const MyForm = () => {
const handleSubmit = (event) => {
event.preventDefault();
const formChildren = event.target.children;
for (let index = 0; index < formChildren.length; ++index) {
if (formChildren[index].nodeName == "INPUT") {
//console.log(formChildren[index].validity); Or you can go this route
if (
formChildren[index].value == "" ||
formChildren[index].value == undefined
) {
console.log(
`Input ${formChildren[index].dataset["userFriendlyInputName"]} cannot be empty`
);
}
}
}
};
return (
<form onSubmit={handleSubmit}>
<input name="name" data-user-friendly-input-name="Name" />
<input name="surname"
/>
{/*
Or you can choose any of those build in parameters
<input name="email"
pattern=""
min-length=""
required=""
/>
*/
}
<button type="submit" />
</form>
);
};
export default MyForm;