javascript how to create a validation error message without using alert
javascript, validation
Solution
You need to stop the submission if an error occured:
HTML
<form name ="myform" onsubmit="return validation();">
JS
if (document.myform.username.value == "") {
document.getElementById('errors').innerHTML="*Please enter a username*";
return false;
}
Problem
I am looking to make a simple form validation error message that displays under the username field. I cannot seem to figure it out. ``` <form name ="myform" onsubmit="validation()"> Username: <input type ="text" name="username" /><br /> <input type ="submit" value="submit" /> <div id ="errors"> </div> </form> ``` Here is my validation script: ``` function validation(){ if(document.myform.username.value == ""){ //checking if the form is empty document.getElementById('errors').innerHTML="*Please enter a username*"; //displaying a message if the form is empty } ```