Leaving the input in focus after javascript valid?

Asked

Viewed 8,816 times

1

I implemented a simple valid 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 what was set in javascript, I already removed the required and still does not work follow the code I am 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>

  • 1

    Here’s a good one reply.

2 answers

3


Just vc put type="button" on the form button:

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  type="button" class="btn btn-danger" onclick="verificaForm()">Verificar ...</button>
   </div>
</form>

Can also put document.getElementById("txtName").style.background = "#D4D4D4"; to better visualize the field in focus. See

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();
        document.getElementById("txtName").style.background = "#D4D4D4";
        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  type="button" class="btn btn-danger" onclick="verificaForm()">Verificar ...</button>
   </div>
</form>

  • it was a mistake of mine not to specify the type in the button, thanks for the validation, in firefox/Chrome and IE perfect but in safari it does not work and did not want to include library wanted to use pure javascript but I think it does not work, already went through this?

  • It didn’t work and I didn’t understand the statement of the variable "var name =", where I use it?

  • This variable "var name =" is there in your code, so you would use it I don’t know.

1

I’ve had this problem in some specific sailing, and another one worked.

I decided to add one timeout, and then set the focus in the field, so:

window.setTimeout(function () { 
    document.getElementById('txtName').focus(); 
}, 0); 

I think it will solve your problem as well, since your code seems to be right.

  • thanks for sharing the information, I believe it should be some problem in safari browser both on iphone and Aple pcs.

  • 1

    Didn’t work? Do a test, put tabindex in its inputs, 0,1,2, etc., in the sequence that the fields must be filled and see if changes the behavior of the browser.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.