How to output error message for error on form validated | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

27th Jul 2020, 1:29 PM
Okonkwo Emmanuel
Okonkwo Emmanuel - avatar
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
27th Jul 2020, 2:13 PM
Ipang
+ 2
I don't see you updated the JS code, it still look like the way it was.
27th Jul 2020, 3:11 PM
Ipang
+ 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
27th Jul 2020, 1:39 PM
Arnesh
Arnesh - avatar
+ 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)
27th Jul 2020, 3:04 PM
Ipang
+ 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 "); } }); }
27th Jul 2020, 3:26 PM
Ipang
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
27th Jul 2020, 2:05 PM
Okonkwo Emmanuel
Okonkwo Emmanuel - avatar
27th Jul 2020, 2:53 PM
Okonkwo Emmanuel
Okonkwo Emmanuel - avatar
0
how should i do that pls
27th Jul 2020, 3:07 PM
Okonkwo Emmanuel
Okonkwo Emmanuel - avatar
0
What do you mean? just edit the code in your code's JS section, that's it.
27th Jul 2020, 3:08 PM
Ipang
0
it didnt work
27th Jul 2020, 3:10 PM
Okonkwo Emmanuel
Okonkwo Emmanuel - avatar