Enable Jquery function when x characters

Asked

Viewed 69 times

2

Very good day!

I have a form with only one input:

    <input type="number" name="codigo" class="form-control" style="margin-left: 2%" autofocus id='codigo'>

And I need that when the input reaches 6 characters it automatically finalizes the form and sends the information to the page on action='', or "emule" the enter button event, or activate the <button type="submit"> is it possible to do such a thing? It is necessary that there be no need for human action to press the button.

Thank you in advance!

1 answer

0


I left two code options on Jquery, when you hit 6 or more characters will be called the button action or submit the form.

Observing: You did not give details of the names of your controls, change to the correct names.

1st solution: We call your button click

$('#inputTexto').on('keyup', function() { 
    if ($(this).val().length >= 6) {
        // Acionamos o clique do seu botão
        $('#btnAcaoEnter').click();
    }
});

2nd solution: We submit the form to the server

$('#inputTexto').on('keyup', function() { 
    if ($(this).val().length >= 6) {
        // Submetemos o fomulário
        $('form').submit();
    }
});

Change the #inputTexto to the id of its input the user will enter the 6 characters.

Change the #btnAcaoEnter to the idfrom your button that wants to trigger the click function when the text input reaches 6 or more characters.

  • Wouldn’t it be better to use $('form').submit() instead of simulating the click on the button?

  • I clicked the button per request on the question, but it’s a good idea. I’ll add in the reply this other solution. Thank you!!

  • I asked why the author of the question does not always know exactly what he needs :)

  • Truth........

  • Thank you very much, helped me a lot and solved my problem!

Browser other questions tagged

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