I want to show an error message when text box is empty on submit, but is not working and everytime I try to get the value through the console area returns undefined.
CSS
<style>
#error_alert {
visibility: hidden;
border: 1px solid #F00;
text-align: center;
width: 100px;
}
label,
input,
button {
display: block;
margin: 2px;
}
</style>
Javascript
<script>
function validate() {
var inputName = document.getElementsByName("fName")
if (inputName.value == " ") {
document.getElementById("error_alert").style.visibility = "visible";
}
}
</script>
Html
<body>
<h1>Titulo</h1>
<p id="error_alert">invalid name</p>
<label>Name:</label>
<input type="text" name="fName" />
<label>e-mail:</label>
<input type="email">
<button onClick="validate()" type="button">Enviar</button>
</body>
PS: When I added x == null ||
as an opntion on the condition the message would turn visible everytime even when i had something written in the textarea.