Javascript Programs
Javascript Programs
Program 2- Perfect/Abundant/Deficient
<!Doctype html>
<html>
<head><title>Perfect/Abundant/Deficient</title></head>
<body>
<script language="JavaScript">
function Check()
{
var i,sum=0;
var num=parseInt(number.value);
alert(num);
for(i=1;i<=num/2;i++)
{
if(num%i==0)
sum=sum+i;
}
if(sum==num)
alert("It's a Perfect number");
else
if(sum>num)
alert("It's an Abundant number");
else
alert("It's a Deficient number");
}
</script>
<form name=form1>
<table>
<tr><td>
Enter the Number</td><td>
<input type="text" name="txtnum" size=10 id="number"></td></tr>
<tr><td colspan="2">
<input type="button" value="Check" onClick="Check()">
</td></tr></table>
</form>
</body>
</html>
</script></head>
<body>
<h1 id="heading">Prompt and Alert Dialogue Boxes</h1>
<p> Program to create an Array and read values using Prompt
popup box and display the sum of elements in an Alert Box</p>
<hr>
<div>
<button type="button" onclick="sum()">Sum of elements using array</button>
</div>
<br></body></html>
Program 8- Change backgroung color of a text box
<!Doctype html>
<html>
<head><title>Change backgroung color of a text box</title></head>
<body>
<script language="JavaScript">
function Addition(){
var c=parseInt(a.value)+parseInt(b.value);
document.form1.textResult.value=c;
}
</script>
<form name=form1 onClick=document.body.style.background="red">
<table>
<tr><td>
Enter number 1:
</td><td><input id="a" type="text" name="txtInt1" size=10 onfocus="this.style.color='Green'"
onblur="this.style.background='pink';" ></td></tr>
<br>
<tr><td>
Enter number 2:
</td><td><input id="b" type="text" name="txtInt2" size=10 onfocus="this.style.color='Green'"
onblur="this.style.background='pink';"></td></tr>
<br>
<tr><td>
Result:
<input id="c" type="text" name="textResult" size=10>
<br>
</td></tr>
<tr><td>
<input type="button" value="+" onClick="Addition();">
</td></tr>
</form>
</body>
</html>
Program 9- Display the capital
<html>
<head><title>To Display the Capital</title></head>
<body>
Select a country :
<select id="countries" onchange=getCapital()>
<option value="0">Australia</option>
<option value="1">Poland</option>
<option value="2">Mexico</option>
<option value="3">Germany</option>
<option value="4">India</option>
</select>
<br/><br/>
Capital: <input type="text" id="txtbox">
<script>
function getCapital() {
var capitals=["Canberra","Warsaw","Mexico City","Berlin","New Delhi"];
var i = document.getElementById("countries").selectedOptions[0].value;
document.getElementById("txtbox").value = capitals[i];
}
</script>
</body>
</html>
Program 10-Loginform validation
<html>
<body>
<script>
function validateform()
{
var name=n1.value;
var password=p1.value;
var cpass=c1.value;
if (name==null || name=="")
{
alert("Name can't be blank");
return false;
}
if(password.length<8)
{
alert("Password must be at least 8 characters long.");
return false;
}
if(password!=cpass)
{
alert("Both password and Confirm password must be same");
return false;
}
}
</script>
<body>
<form name="myform" method="post"
action="Confirm.html" onsubmit="return validateform()" >
<table>
<tr><td>Name:</td>
<td><input type="text" name="name" id="n1"></td></tr>
<tr><td>Password</td>
<td><input type="password" name="password" id="p1"></td></tr>
<tr><td>Confirm Password</td>
<td><input type="password" name="cpassword" id="c1"></td></tr>
<tr><td><input type="submit" value="register"></td></tr></table>
</form>
</body>
</html>
Confirm.html
<!doctype html>
<html>
<head>
<title>Confirmation page</title>
</head>
<body>
<H1> You are a valid user </H!>
</body>
</html>