Slip 16 - B) Write Ajax program to carry out validation for a username entered in textbox. If the textbox is blank, print ‘Enter username’, If the number of characters is less than three, print’ Username is too short’. If value entered is appropriate the print ‘Valid username’.

 Solution:


Html File:

<html>
<head>
<script type="text/javascript">
function showval(str) {
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("result").innerHTML = this.responseText;
}
};
xhttp.open("GET", "slip17-p1-q2.php?txt="+str, true);
xhttp.send(); 
}
</script>
<body>
<form action=""> 
User name: <input type="text" name="txt" onkeyup="showval(this.value)">
</form>
<div id='result'></div>
</body>
</html>

PHP File:

<?php
$user=$_GET['txt'];
if($user==="")
$str="Enter user name";
else if(strlen($user)<3)
$str="User name is too short";
else
$str="Valid username";
echo $str=== ""? "no suggestion":$str;
?>

Post a Comment

0 Comments