+ 1
How to output error message for error on form validated
I tried to output an error message using JavaScript, the form validator worked but Iâm not seeing the message, if I use alert it will work but if I use an array to push the message to the user it wonât show the message.
10 Answers
+ 3
I think things could be clearer if only you just share your code link as Arnesh said. Follow the below guide on how to share link đ
https://www.sololearn.com/post/75089/?ref=app
+ 2
I don't see you updated the JS code, it still look like the way it was.
+ 1
I don't completely get what you mean. Can you please share your code? If you wanted to know about raising errors.. Use the throw keyword:
https://code.sololearn.com/WVyI2Kv5tXRz/?ref=app
+ 1
Try these steps:
1. Wrap your current JS code in a load event handler
window.onload = () =>
{
// your JS code here
}
2. Check for number of <messages> element instead of comparing it to a number
if(messages.length > 0)
+ 1
I meant it to be like this actually
window.onload = () => {
const names = document.getElementById("names");
const password = document.getElementById("password");
const form = document.getElementById("form")
const errorElement = document.getElementById("error");
form.addEventListener("submit", (e) => {
let messages = [];
if(names.value.length < 2) {
messages.push("Name is required")
}
if(password.value.length < 8) {
messages.push("Password must be at least 8 characters long")
}
if(messages.length > 0) {
e.preventDefault();
errorElement.innerText = messages.join(" and ");
}
});
}
0
thanks but what i want to do is, form validation, if there is a false input the user should get an error message, after writing my code the error message is not showing, instead of using alert(ââ)
i used an array, if the input is wrong a message should be pushed to the array that will be shown to the user
0
https://code.sololearn.com/WCQmGd0ypRwH/?ref=app
thats the code
0
how should i do that pls
0
What do you mean? just edit the code in your code's JS section, that's it.
0
it didnt work