jason5137715
jason5137715

Reputation: 117

How to setup length in search box

I have search box connected to mysql database.

I'am trying to set the min charchters in search box to be 3 letters to start a search, but I want it at the same time accept search by these two letters "th" together.

I'm trying to setup the length like that but it doesnt work! Here is my code:

$('#search_text').keyup(function(){
  var search = $(this).val();
  var minlength=3;
  if(search != '') {
    if (event.key === "Enter") {
      load_data(search);
    } else {
      return;
    }
  }
});
});

Upvotes: 0

Views: 35

Answers (2)

Jack Bashford
Jack Bashford

Reputation: 44145

Just add another condition:

if (search != "" && (search.startsWith("th") || search.length >= minlength)) {...}

Upvotes: 2

lmiller1990
lmiller1990

Reputation: 945

Not sure what you mean by 'th' together - should 'th' just be counted as one letter?

For the min length, you can check like this:

$('#search_text').keyup(function(){
  var search = $(this).val();
  var minlength = 3;
    if (event.key === "Enter" && search.length >= minlength) {
      load_data(search);
    }
  });
});

If you wanted to count 'th' as one letter:

var count = (search.match(/th/g) || []).length // returns no. of occurrences of 'th'
var totalCount = search - count

Upvotes: 1

Related Questions