I am making a form that if the user leave the field name blank, display the message in the field, rather than an alert. document.write
does not work with function through an event. I'm using innerHTML
and textContent
. However, I can not display the message. I made two codes, let's see:
<html>
<body>
<script>
function validar() {
if (document.formulario.nome.value.length == 0) {
var alerta = "O nome deve ser informado";
var aviso.innerHTML = alerta;
}
}
</script>
<form name="formulario">
Nome: <input type="text" name="nome">
<input type="submit" value="Enviar" name="cadastro" onclick="validar()">
</form>
</body>
</html>
Second code:
<html>
<body>
<script>
function validar() {
if (document.formulario.nome.value.length == 0) {
var alerta = document.getElementById('alerta');
alerta.textContent = "O nome deve ser informado";
}
}
</script>
<form name="formulario">
Nome: <input type="text" name="nome"> <input type="submit"
value="Enviar" name="cadastro" onclick="validar()">
</form>
</body>
</html>