I am trying to validate field when I click on submit button. This is how I defined my state.
const [values, setValues] = useState({
email: "",
password: "",
emailErrorMessage: "",
passwordErrorMessage: "",
errorMessage: "",
emailError: false,
passwordError: false,
rememberMe: false,
});
In submit method, this is how I am validating it.
if (values.email.length <= 0) {
setValues({
...values,
emailError: true,
emailErrorMessage: "Email field must not be empty"
})
};
if (values.password.length <= 0) {
setValues({ ...values, passwordError: true, passwordErrorMessage: "Password field must not be empty" });
}
For somehow it is only showing me error for password field. Although it is going through both the if conditions. Not sure how exactly I can Fix it.
Expected:- it should show error for both email and password.