I implemented a simple validation to check if the input matches the rules and if it is negative it should be in focus to change the data, but the focus is not working, it leaves the next item in focus and not the one that was set in the javascript, I already removed the required one and it does not work, it follows the code I'm using.
function verificaForm() {
//var doc = document.getElementById('txtName').value;
//alert(doc);
if (document.formulario.txtName.value === "") {
alert('Campo nome vazio');
//document.getElementById('txtName').focus();
var nome = document.getElementById('txtName');
document.getElementById('txtName').focus();
return false;
}
return true;
}
<form class="form" name="formulario" method="post">
<div class="form-control">
<label for="txtName">Nome:</label>
<input type="text" id="txtName" name="txtName">
</div>
<div class="form-control">
<label for="txtSobrenome">Sobre-nome</label>
<input type="text" id="txtSobrenome" name="txtSobrenome">
</div>
<div class="form-control">
<label for="txtIdade">Idade</label>
<input type="text" id="txtIdade" name="txtIdade">
</div>
<div class="form-control">
<button class="btn btn-danger" onclick="verificaForm()">Verificar ...</button>
</div>
</form>