PHP File-->app2.php
<?php include("common.php"); ?>
<?php
$z=$_GET["q"];
$final=$z;
if (strlen($z) > 0)
{
$z=substr($z,-1);
$prompt="";
if(preg_match('/[^a-zA-Z0-9\\s\\_]/', $z))
{
$prompt="Please enter only letters, numbers, underscores, or spaces.";
}
echo $prompt;
}
?>
HTML File
<html>
<title>My Ajax Application</title>
<body>
<P>
<B>
<script type="text/javascript">
function prompt(str)
{
if (str.length==0)
{
document.getElementById("prompt").innerHTML="";
return;
}
document.getElementById("prompt").innerHTML="";
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("prompt").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","app2.php?q="+str,true);
xmlhttp.send();
}
</script>
</B>
</P>
<P>
<B>
<div class='k'><p><b>Type input. Rule: only alphanumerics, space and underscore are OK.</b></p>
<form>
Input: <input type="text" onkeyup="prompt(this.value)" size="20" />
</form>
<p>Prompt: <span id="prompt"></span></p>
</div>
</body>
</html>
Source-->http://css-resources.com/Ajax-and-PHP-Based-Input-Filter.html
The above code is works fine,which is allow alphanumeric,spaces and uderscore for your text box