Okay so I have 1 states in React.js, with 2 key,val pairs. Length validation & character validation as shown below:
const [validation, setValidationState] = useState({
lengthValidation: "",
characterValidation: "",
});
These 2 states are wrapped in a function named promptValidation ( I'll show all code at the end )
Within this function I have 2 conditionals, in which only 1 works at a time. Those conditionals are shown below:
// if password length doesn't meet criteria set state to error message
if (
props.promptstate["length"] < 8 ||
props.promptstate["length"] > 128 ||
Number.isNaN(props.promptstate["length"])
) {
setValidationState({
...validation,
["lengthValidation"]:
"Password length must be between 8-128 characters...",
});
} else {
setValidationState({
...validation,
["lengthValidation"]: "",
});
}
// if password characters aren't selected at all set state to error message
if (
!props.promptstate["specialCharacters"] &&
!props.promptstate["upperCase"] &&
!props.promptstate["lowerCase"] &&
!props.promptstate["numbers"]
) {
setValidationState({
...validation,
["characterValidation"]:
"At least one character type should be selected...",
});
}
// if password does meet require set state back to empty string
else {
setValidationState({
...validation,
["characterValidation"]: "",
});
}
Now my issue is upon invoking my function on a button click only one of the conditionals run. And it's pretty weird, considering it's the second conditional that runs.
The weirder part is, if I comment out one conditional and my function meets that conditional it will run, if I switch the commenting of the other conditional and my function meets criteria then that will run as well.
The issue present it's self when both conditionals are in play.
My expected behavior is for both conditionals to update the state upon both of them meeting the conditionals criteria.
Below is the the state and full function that's being ran on click.
const [validation, setValidationState] = useState({
lengthValidation: "",
characterValidation: "",
});
const promptValidation = () => {
// if password length doesn't meet criteria set state to error message
if (
props.promptstate["length"] < 8 ||
props.promptstate["length"] > 128 ||
Number.isNaN(props.promptstate["length"])
) {
setValidationState({
...validation,
["lengthValidation"]:
"Password length must be between 8-128 characters...",
});
} else {
setValidationState({
...validation,
["lengthValidation"]: "",
});
}
// if password characters aren't selected at all set state to error message
if (
!props.promptstate["specialCharacters"] &&
!props.promptstate["upperCase"] &&
!props.promptstate["lowerCase"] &&
!props.promptstate["numbers"]
) {
setValidationState({
...validation,
["characterValidation"]:
"At least one character type should be selected...",
});
}
// if password does meet require set state back to empty string
else {
setValidationState({
...validation,
["characterValidation"]: "",
});
}
console.log(validation);
};
Please assist if possible, thank you. I'm a noob when it comes to react & es6.