Focus on the first validation element - jQuery

Asked

Viewed 82 times

2

Hello,

With the function below, I validated required fields.

In this case, I don’t use this function to submit the form, but rather to add items to the object.

However, I want to focus on the first mandatory element.

Something like: $('#elemento')[0].focus.

Can you help me?

campos = ['#txt-cep', '#txt-logradouro', '#txt-bairro', '#txt-cidade', '#txt-uf'];

if (campo_obrigatorio(campos)) {
    //faça algo
}

function campo_obrigatorio(campos) {
    var ret = true;
    for (i = 0; i < campos.length; i++) {
        var $form_line = $(campos[i]).parent();
        if (!$(campos[i]).val()) {		
            if (!$form_line.hasClass('error focused')) {
                $form_line.addClass('error focused');
    		//$form_line.find($(campos[i]).focus());
             }
             ret = false;
        }
        else {
            if ($form_line.hasClass('error focused')) {
                $form_line.removeClass('error focused');
            }
        }
    }
    return ret;
}

1 answer

1


The error is on this line:

$form_line.find($(campos[i]).focus());

The .focus() should be outside the parentheses of .find:

$form_line.find($(campos[i])).focus();

However, the way you are doing will not select the first, but always the last.

In this case, select the first element by the classes after the completion of the loop for:

$(".error.focused:eq(0) input").focus();

Example:

campos = ['#txt-cep', '#txt-logradouro', '#txt-bairro'];

if (campo_obrigatorio(campos)) {
    //faça algo
}

function campo_obrigatorio(campos) {
    var ret = true;
    for (i = 0; i < campos.length; i++) {
        var $form_line = $(campos[i]).parent();
        if (!$(campos[i]).val()) {
            if (!$form_line.hasClass('error focused')) {
                $form_line.addClass('error focused');
             }
             ret = false;
        }
        else {
            if ($form_line.hasClass('error focused')) {
                $form_line.removeClass('error focused');
            }
        }
    }
    
    $(".error.focused:eq(0) input").focus();
    
    return ret;
}
.error.focused{
   background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
   <p>
      <input type="text" id="txt-cep" value="texto">
   </p>
   <p>
      <input type="text" id="txt-logradouro">
   </p>
   <p>
      <input type="text" id="txt-bairro">
   </p>
</form>

  • thanks for the feedback, but if I keep $(campos[i]) will always focus on the last mandatory element and if I change to $(campos[0]) will focus only on the first field, and if I fill the first field, the focus does not move to the second required field.

  • Better, it was missing the false Return, now it worked. However the class error focused only applied to the field with focus, would be able to apply in all mandatory fields and keep the focus on the first unfilled Camo ?

  • I changed the answer.

  • It worked great, thanks @Sam..

Browser other questions tagged

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