<input class="form-control" type="text" name="titulo" id="titulo" />
<div id="tituloPost"></div>
<textarea class="form-control" name="contenido" id="contenido" rows="4" cols="50" placeholder="Escribe tu mensaje aquÃ" ></textarea>
<div id="mensaje"></div>
And the button
<button type="submit" class="btn btn-info" value="Validar campos" onclick="return validarCampos()" style="margin-bottom: 5px;">Publicar nuevo tema</button>
I have this function that checks two input
function validarCampos(){
var valido = true;
var titulo = document.getElementById("titulo").value;
var testTitulo = /^[\W\s\w]{1,100}$/i;
var mensaje = document.getElementById("contenido").value;
var testMensaje = /^[\W\s\w]{1,2000}$/i;
if (testTitulo.test(titulo)) {
valido = true;
} else{
valido = false;
document.getElementById("tituloPost").innerHTML = "Este campo no debe de estar vacÃo ni superar los 100 caracteres";
}
if (testMensaje.test(mensaje)) {
valido = true;
} else {
valido = false;
document.getElementById("mensaje").innerHTML = "Este campo no debe de estar vacÃo ni superar los 2000 caracteres";
}
return valido;
}
The problem is that when one of the two fields is filled and the other empty it sends me the information and it should not be like that.