I have a search box and it does not have this typical submit button. It looks like this:

HTML:
<div class="input-group">
<input type="text" class="form-control" name="keyword" id="searchbox" onkeypress="return checkLength()"/>
<span class="btn btn-primary input-group-addon" onclick="checkLength()"><i class="fa fa-search"></i></span>
</div>
I added only the item span
, not the item input
for the submit button. How can I check if the user enters or enters at least two characters? If the user enters only 1 character, then presses this search button or simply presses the enter key, a red error message โKeyword must be at least two charactersโ or something like that should appear at the bottom of the search field.
I tried this code but it does not work:
function checkLength(){
var textbox = document.getElementById("searchbox");
if(textbox.value.length <= 10 && textbox.value.length >= 2){
alert("success");
} else {
alert("Keyword should be not less than 2 characters");
$(document).keypress(function (e) {
var keyCode = (window.event) ? e.which : e.keyCode;
if (keyCode && keyCode == 13) {
e.preventDefault();
return false;
}
});
}
}
Need help. Thanks.
EDIT:
, , 2 , , . ?