How to focus the input of a form constantly?

2

I have a form and need to focus on an input again until it satisfies the required, eg:

$('#text1').blur(function() {
  var teste = $('#text1').val().length;
  console.log(teste);
  if(teste > 5){
    $('#text1').focus();
  }else{
    $('#text2').focus();
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><label>Input1:<inputtype="text" name="text1" id="text1">
</label>
<br><br>
<label>
Input 2:
<input type="text" name="text2" id="text2">
</label>

Is it possible to do this?

    
asked by anonymous 15.12.2017 / 04:40

1 answer

2

In this case you should use .focus since #

  • $('#text2').focus(function() {
      $('#text1').focus();
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><label>Input1:<inputtype="text" name="text1" id="text1">
    </label>
    <br><br>
    <label>
    Input 2:
    <input type="text" name="text2" id="text2">
    </label>
        
    15.12.2017 / 04:45