I am trying to add error message handling in Javascript but am having trouble. If a user inputs a state that is not two characters in length, I am trying to have it output an error message. I also am including my renderBarChart function too if that helps.
js
stateSubmitButton.addEventListener("click", function(event) {
event.preventDefault();
state = stateUserInput.value.toUpperCase();
let stateFeedbackElem = document.querySelector("#stateFeedback");
if (state.length == 2) {
stateUserInput.setCustomValidity("");
stateFeedbackElem.textContent = "";
renderBarChart(state);
state = "";
} else {
stateUserInput.setCustomValidity("Please enter a two letter abbreviated state.");
stateFeedbackElem.textContent = "Please enter a two letter abbreviated state.";
state = "";
}
})
html
<form class="form" novalidate>
<label for="state-input">State:</label>
<input name="state" type="text" id="state-input" placeholder="(i.e. WA)">
<button type="submit" id="state-submit" class="btn btn-dark btn-lg ml-2">Submit</button>
<div id="stateFeedback" class="invalid-feedback"></div>
</form>