4

I have HTML code like

<input type="text" class="input-box">

I want to execute a jQuery function when the inputbox is filled with 4 character.

For example, I type "ABCD" then the function will be executed and not for "AB" or "ABC" something like that. How can I do this?

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
Shafeeque S
  • 2,533
  • 3
  • 15
  • 19

1 Answers1

5

Bind an event handler and check text length, based on the length call the function

$('.input-box').on('input', function() {
  if (this.value.trim().length >= 4) {
    // call your function here
    doIt();
  }
});

function doIt() {
  alert('hi');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="input-box">
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188