Reputation: 95
I have a form in which the form should automatically search when I complete entering the 10th character in the text field but the below code is searching for each n every character I enter in the text field . . . I just want the result after completing the 10th character not for each n every character . . I have used onkeyup
event and I set that value to 10 but still it is searching for each n every character... please do help me
<body OnKeyPress="return disableKeyPress(event)">
<section id="content" class="container_12 clearfix" data-sort=true>
<center><table class='dynamic styled with-prev-next' data-table-tools='{'display':true}' align=center>
<script>
function disableEnterKey(e)
{
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
return (key != 13);
}
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","resdb.php?id="+str,true);
xmlhttp.send();
}
</script>
<script type='text/javascript'>
//<![CDATA[
$(window).load(function(){
$('#id').keyup(function(){
if(this.value.length ==10)
});
});//]]>
</script>
<form id="form" method="post" name="form" >
<tr><td><p align="center"><font size="3"><b>JNTUH - B.Tech IV Year II Semester (R07) Advance Supplementary Results - July 2012</b></font></p></td></tr>
<td><p align="center"><b>Last Date for RC/RV : 8th August 2012</b></p></td>
<tr><td><p align="center"></b> <input type="text" onkeyup="showUser(this.value)" onKeyPress="return disableEnterKey(event)" data-type="autocomplete" data-source="extras/autocomplete1.php" name="id" id="id" maxlength="10" placeholder="Hall-Ticket Number"> </p></td></tr>
</form>
</center>
</table>
<center>
<div id="txtHint"><b>Results will be displayed here</b></div>
</center>
</body>
Upvotes: 1
Views: 2908
Reputation: 2845
This is an idea as your code is working. Do it this way. First check your textfield if it is more than 10 character or not. call fnc() onkeyup
function fnc()
{
length=document.getElementById("atext").value.length;
if(length==10)
{
callyourajaxfunction()
}
}
Upvotes: 1