0% found this document useful (0 votes)
367 views

Javascript Programs PDF

The document contains 11 JavaScript programs that demonstrate various programming concepts: 1. A mini calculator program that performs basic arithmetic operations 2. A program that checks if a number is Armstrong or not 3. A program that displays the Fibonacci series up to a given number 4. Additional programs check for the largest of three numbers, perfect numbers, palindromes, strong numbers, prime numbers, and sorting and summing values.

Uploaded by

Ravi Shankar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
367 views

Javascript Programs PDF

The document contains 11 JavaScript programs that demonstrate various programming concepts: 1. A mini calculator program that performs basic arithmetic operations 2. A program that checks if a number is Armstrong or not 3. A program that displays the Fibonacci series up to a given number 4. Additional programs check for the largest of three numbers, perfect numbers, palindromes, strong numbers, prime numbers, and sorting and summing values.

Uploaded by

Ravi Shankar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

JAVASCRIPT PROGRAMS

Conditional Statements:
1. Javascript program on arithmetic operations
<html>
<head><title>Arithmetic Operations</title>
<script language = "javascript">
var res;
function add()
{
var s1 = parseInt(document.f1.t1.value);
var s2 = parseInt(document.f1.t2.value);
res = s1+ s2;
document.f1.res.value = res;
}
function sub()
{
var s1 = parseInt(document.f1.t1.value);
var s2 = parseInt(document.f1.t2.value);
res = s1- s2;
document.f1.res.value = res;
}
function mul()
{
var s1 = parseInt(document.f1.t1.value);
var s2 = parseInt(document.f1.t2.value);
res = s1 * s2;
document.f1.res.value = res;
}
function div()
{
var s1 = parseInt(document.f1.t1.value);
var s2 = parseInt(document.f1.t2.value);
res = s1/ s2;
document.f1.res.value = res;
}
</script></head>
<body>
<br><br><br><h2>MINI CALCULATOR PROGRAM</h2>
<br><br><form name = "f1">
<font size = 03 face = "Courier New">
<b>Enter first number : <input type = "text" size = "10" name = "t1"><br><br>
Enter second number : <input type = "text" size = "10" name = "t2"><br><br>
Result is : <input type = "text" size = "10" name = "res">
<br><br></b></font>
<INPUT TYPE="button" value="Addition" onclick = "add()">
<INPUT TYPE="button" value="Subtraction" onclick = "sub()">
<INPUT TYPE="button" value="Multiplication" onclick = "mul()">
<INPUT TYPE="button" value="Division" onclick = "div()">
</form>
</body></html>

2. Program to check whether given number is armstrong or not


<html>
<head><title>Armstrong Number</title>
<script language = "javascript">
var x, y, sum=0, k;
y = x;
function arm()
{
x = parseInt(document.fr.n.value);
while(x!=0)
{
k = parseInt(x)%10;
x = Math.floor(x/10);
sum = sum+Math.pow(k,3);
}
if(sum == y)
window.alert("Number is armstrong");
else
window.alert("Number is not armstrong");
}
</script></head>
<body>
<br><br><br>
<h2>Program to find whether given number is armstrong or not</h2>
<br><br><br><h4>Output Shield</h4>
<form name="fr"><font size = 04 face = "Bookman Old Style">
Enter Number: <input type = "text" size = "15" name = "n">
<br><br><br>
<input type = "button" value = "Check" onclick = "arm()">
</font>
</form></body></html>

3. Program to display fibonacci series


<html>
<head><title>Fibonacci Series</title>
<script language = "javascript">
var f1 = 0, f2 = 1, f3 = 0, n;
function fibo()
{
var n = parseInt(document.fo.nn.value);
document.write("<h1>"+f1);
do
{
f1 = f2;
f2 = f3;
f3 = f1+ f2;
document.write("<h1> "+f3+" ");
}
while(f3 < n);
}
</script></head>
<body>
<br><br><br><br>
<h2>Program to display n below Fibonacci series</h2>
<br><br><h3>Ouput Shield</h3>
<br><br><font size = 04 face = "Bookman Old Style">
<form name = "fo">
Enter number : <input type = "text" size = "12" name = "nn">
<br><br><br>
<input type = "button" value = "Display" onclick = "fibo()">
</font>
</form></body></html>

4. Program to display the largest number


<html>
<head><title>Output to find the largest of three numbers</title>
<script language = "javascript">
function largest()
{
var a, b, c;
a = parseInt(document.lar.fir.value);
b = parseInt(document.lar.sec.value);
c = parseInt(document.lar.thd.value);
if (a>b && a>c)
{
window.alert("The largest number is "+a+"");
}
if (b>c && b>a)
{
window.alert("The largest number is " +b);
}
if (c>a && c>b)
{
window.alert("The largest number is " +c);
}
}
</script></head>
<body>
<br><br><br>
<h1>Program to find the largest of three numbers</h1>
<br><br><h3>Output Shield</h3><br>
<form name = "lar">
<font size = 04 face = "Bookman Old Style">
Enter first number : <input type = "text" size = 15 name = "fir"><br>
<br>Enter second number : <input type = "text" size = 15 name = "sec"><br>
<br>Enter third number : <input type = "text" size = 15 name = "thd"><br><br><br>
<input type = button value = "Find Largest" onclick = "largest()">
</font>
</form></body></html>

5. Program to check whether given number is perfect or not


<html>
<head><title>Perfect Number</title>
<script language = "javascript">
var n, sum=0, k;
i=1;
function perfect()
{
x = parseInt(document.fr.n.value);
while(i <= n/2)
{
if(n%i == 0)
sum = sum+i;
i++;
}
if(sum == n)
window.alert("Number is perfect");
else
window.alert("Number is not perfect");
}
</script></head>
<body>
<br><br><br>
<h2>Program to find whether given number is perfect or not</h2>
<br><h3>Output Shield</h3><br><br>
<form name="fr">
<font size = 04 face = "Bookman Old Style">
Enter Number: <input type = "text" size = "15" name = "n">
<br><br><br>
<input type = "button" value = "Check" onclick = "perfect()">
</font>
</form></body></html>

6. Program to check whether a given number is palindrome or not


<html>
<head><title>Number Palindrome</title>
<script language = "javascript">
var x, p, sum = 0, r;
function palincheck()
{
p = parseInt(document.pal.num.value);
x = p;
window.alert("Given number is " + p);
while(p > 0)
{
sum = sum * 10;
sum = sum + parseInt(p%10);
p = parseInt(p/10);
}
window.alert("The Reverse number is " + sum);
if (x == sum)
window.alert(sum + " is Palindrome");
else
window.alert(sum + " is not palindrome");
}
</script></head>
<body>
<br><br><br>
<h2>Program to find whether the given number is palindrome or
not</h2><br><br><h3>Output Shield</h3><br>
<form name = "pal">
<font size = 04 face = "Bookman Old Style">
Enter number : <input type = "text" size = 15 name = "num"><br>
<br><br><input type = button value = "Check" onclick = "palincheck()">
</font>
</form></body></html>

7. Program to check whether a given number is strong or not


<html>
<head><script language="javascript">
var x,n,sum,fact,rem;
x=parseInt(window.prompt("Enter a number:", "0"));
n=x;
sum=0;
while(x!=0)
{
rem=x%10;
fact=1;
while(rem!=0)
{
fact=fact*rem;
rem--;
}
sum=sum+fact;
x=Math.floor(x/10);
}
if(sum==n)
document.write("<h1>Number is strong");
else
document.write("<h1>Number is not strong");
</script></head></html>

8. Program to display n below prime numbers


<html>
<head><title>Prime Numbers</title>
<script language = "javascript">
var n, i = 2, j = 1;
function prime()
{
n = parseInt(document.fr.n1.value);
for (i = 2; i<=n; i++)
{
var c = 0;
for(j = 1; j<=n; j++)
{
if(i % j == 0)
c++;
}
if(c == 2)
document.writeln(" "+i+" ");
}
}
</script></head>
<body>
<br><br><br><br>
<h2>Program to display n below prime numbers</h2>
<br><br>
<h4>Output Shield</h4>
<br><br>
<font size = 04 face = "Bookman Old Style"><form name = "fr">
Enter number : <input style = "text" name = "n1" size = 12>
<br><br><input type = "button" value = "Display" onclick = "prime()">
</form>
</body>
</html>

9. Program to check whether a given number is prime or not


<html>
<head><title>Check for Prime Number</title>
<script language = "javascript">
var n, i = 1, c = 0;
function check()
{
n = parseInt(document.fr.n1.value);
while(i<=n)
{
if(n%i == 0)
c++;
i++;
}
if(c <= 2)
document.write("<h1>Prime</h1>");
else
document.write("<h1>Not Prime</h1>");
}
</script></head>
<body>
<br><br><br><br>
<h2>Program to check whether the given number is prime or not</h2>
<br><br><h4>Output Shield</h4><br><br>
<font size = 04 face = "Bookman Old Style"><form name = "fr">
Enter number : <input style = "text" name = "n1" size = 12>
<br><br><input type = "button" value = "Check" onclick = "check()">
</form>
</body></html>

10. Program to sort three numbers in ascending order


<html>
<head><title>Output to sort three numbers in ascending order </title>
<script language = "javascript">
function sort()
{
var a, b, c, i=1, sum;
a = parseInt(document.asc.fir.value);
b = parseInt(document.asc.sec.value);
c = parseInt(document.asc.thd.value);
sum = a+b+c;
while(i<=sum)
{
if(i == a||i == b||i == c)
document.write(" "+i+" ");
i++;
}
}
</script></head>
<body>
<br><br><br>
<h1>Program to arrange 3 numbers in ascending order</h1>
<br><br><h3>Output Shield</h3><br><form name = "asc">
<font size = 04 face = "Bookman Old Style">
Enter first number : <input type = "text" size = 15 name = "fir"><br>
<br>Enter second number : <input type = "text" size = 15 name = "sec"><br>
<br>Enter third number : <input type = "text" size = 15 name = "thd"><br><br><br><input
type = button value = "Sort" onclick = "sort()">
</font>
</form></body></html>

11. Program to display sum of n below numbers


<html>
<head><title>Numbers Sum</title>
<script language = "javascript">
var n, sum = 0;
function add()
{
n = parseInt(document.fr.n1.value);
while(n!=0)
{
sum = sum + n;
n--;
}
document.write("<h2><b>The sum of n below numbers = </b>" +sum);
}
</script></head>
<body>
<br><br><br><br><h2>Program to display n below numbers sum</h2>
<br><br><h4>Output Shield</h4><br><br>
<font size = 04 face = "Bookman Old Style"><form name = "fr">
Enter number : <input style = "text" name = "n1" size = 12>
<br><br><input type = "button" value = "Display" onclick = "add()">
</form>
</body></html>

12. Program to display nth table


<html>
<head><title>Mutliplication table</title>
<script language = "javascript">
var a, i=1;
function table()
{
a = parseInt(document.fr.n.value);
do
{
document.write("<h3>"+a+" X "+i+" = "+a*i+"<\h3>");
i++;
}
while(i<=10);
}
</script></head>
<body>
<br><br><br><h2>Program to display the nth table</h2>
<br><h3>Output Shield</h3><br><br><form name="fr">
<font size = 04 face = "Bookman Old Style">
Enter Number: <input type = "text" size = "15" name = "n">
<br><br><br>
<input type = "button" value = "Display Table" onclick = "table()">
</font>
</form></body></html>
13. Program to find factorial of a given number
<html>
<head><title>Factorial of a number</title>
<script language = "javascript">
var a, i;
f=1;
function fact()
{
a = parseInt(document.fr.n1.value);
for(i=a;i>=1;i--)
{
f = f * i;
}
document.write("<h1>The factorial of given number is " + f);
}
</script></head>
<body>
<br><br><br><br><h2>Program to find factorial of a given number</h2>
<br><br><h4>Output Shield</h4><br><br>
<font size = 04 face = "Bookman Old Style"><form name = "fr">
Enter number : <input style = "text" name = "n1" size = 12>
<br><br><input type = "button" value = "Factorial" onclick = "fact()">
</form>
</body></html>

14. Program for reversing a number


<html>
<head><title>Reverse a number</title>
<script language = "javascript">
var n, v=0, r;
function reverse()
{
n = parseInt(document.fr.n1.value);
while(n > 0)
{
v = v * 10;
v = v + parseInt(n%10);
n = parseInt(n/10);
}
document.write("<h1>The Reverse number is " + v);
}
</script></head>
<body>
<br><br><br><br><h2>Program for reversing a given number</h2>
<br><br><h4>Output Shield</h4><br><br>
<font size = 04 face = "Bookman Old Style">
<form name = “fr">
Enter number : <input style = "text" name = "n1" size = 12>
<br><br><input type = "button" value = "Reverse" onclick = "reverse()">
</form>
</body></html>

Examples on Arrays:
1. Program to input n numbers display in sorting order
<html>
<head><script language = "javascript">
var n, i, j, temp;
n = parseInt(window.prompt("Enter value of n:", "0"));
var a = new Array(n);
for(i=0; i<n; i++)
a[i] = parseInt(window.prompt("Enter "+i+"th element:", "0"));
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
document.write("<h1>Sorted elements are:</h1>");
for(i=0; i<n; i++)
document.write("<h1>"+a[i] + " ");
</script></head>
</html>

2. Program to input n numbers apply binary search


<html>
<head><script language = "javascript">
var j, n, num, mid, s=0, e;
n = parseInt(window.prompt("Enter number:"));
var item = new Array(n);
for(j=0; j<n; j++)
{
item[j] = parseInt(window.prompt("Enter "+j+"th element:"));
}
num = parseInt(window.prompt("Enter element to be searched:"));
e = n-1;
mid = Math.floor((s+e)/2);
while(num != item[mid] && s<=e)
{
if(num>item[mid])
s = mid+1;
else
e = mid-1;
mid = Math.floor((s+e)/2);
}
if(num == item[mid])
document.write("<br><h1>"+num+" is found at position "+(mid+1)+"<br>");
if(s>e)
document.write("<br><h1>"+num+" is not found.</h1>"+"<br>");
</script></head>
</html>

3. Program for matrix multiplication


<html>
<head><script language = "javascript">
var a,b,c,i,j,k,m,n,p,q;
var s = " ";
m = parseInt(window.prompt("Enter no.of rows for matrix A: "));
n = parseInt(window.prompt("Enter no.of columns for matrix A: "));
p = parseInt(window.prompt("Enter no.of rows for matrix B: "));
q = parseInt(window.prompt("Enter no.of columns for matrix B: "));
a = new Array(m);
for(i=0; i<m; i++)
a[i] = new Array(n);
b = new Array(p);
for(i=0; i<p; i++)
b[i] = new Array(q);
c = new Array(m);
for(i=0; i<m; i++)
c[i] = new Array(q);
if(p == n)
{
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
a[i][j] = parseInt(window.prompt("Enter matrix A elements:"));
}
for(i=0; i<p; i++)
{
for(j=0; j<q; j++)
b[i][j] = parseInt(window.prompt("Enter matrix B elements:"));
}
for(i=0; i<m; i++)
{
for(j=0;j<q; j++)
{
c[i][j] = 0;
for(k=0; k<n; k++)
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
}
}
document.write("<h1>Matrix is <br>" );
document.write("<br>");
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
{
document.write("<h1>"+c[i][j]+" </h1>");
document.write("<br>");
}
}
}
else
document.write("<h1>Matrix Multiplication not possible</h1>");
</script></head>
</html>

4. Program to input n elements display their sum


<html>
<head><script language = "javascript">
var a = new Array(10);
var s = 0, i;
for (i=0; i<a.length; i++)
a[i] = parseInt(window.prompt("Enter the numbers:","0"));
for(i=0; i<a.length; i++)
s = s + a[i];
document.write("<h1>Sum = " + s);
</script></head>
</html>

5. Program to display the largest number


<html>
<head><script language = "javascript">
var n, i, max;
function max()
{
n = parseInt(document.fr.n1.value);
var a = new Array(n);
for (i=0; i<a.length; i++)
{
a[i] = parseInt(window.prompt("Enter the numbers:", "0"));
}
max = parseInt(a[0]);
for(i=1; i<n; i++)
{
if(a[i] > max)
max = parseInt(a[i]);
}
document.write("<h1>Maximum value = " + max);
}
</script></head>
<body>
<br><br><h1>Program to input n elements to find the maximum value</h1>
<br><br><h3>Output Shield</h3><br><br>
<form name ="fr"><h4> Enter number: <input type = "text" size = 12 name = "n1">
<br><br><input type = "Button" value = "Find" onclick = "max()">
</form>
</body></html>

6. Program to input a matrix and display its transpose


<html>
<head><script language = "javascript">
var a = new Array(3);
var i, j, t;
for(i=0; i<3; i++)
a[i] = new Array(3);
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
a[i][j] = parseInt(window.prompt("Enter matrix elements:", "0"));
}
for(i=0; i<3; i++)
{
for(j=1; j<3; j++)
{
if(i != j)
{
t = a[i][j];
a[i][j] = a[j][i];
a[j][i] = t;
}
}
}
document.write("<h1>Transpose of given matrix:");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
document.write("<h1>"+a[i][j]+" </h1>");
document.write("<br>");
}
}
</script></head>
</html>

To obtain date and time of a system using Date function:


1. Program for date and time in head tag.
<html>
<head>
<pre>
<script language = “javascript”>
var mydate = new Date();
document.write(“Today is : ”, +mydate.toString());
</script>
</pre></head>
</html>

2. Program for date and time in body tag and script in head tag.
<html>
<head><pre>
<script language = "javascript">
function start()
{
var mydate = new Date();
c.innerText= "Today is : "+mydate.toString();
}
</script>
</pre></head>
<body bgcolor = "wheat" onload="start()">
<font face = verdana size = 3 color = darkblue>
<h3 id = c></h3></font>
</body>
</html>

3. Program for Clock created with timing event


<html>
<head><script type="text/javascript">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script></head>
<body onload="startTime()">
<div id="txt"></div>
</body>
</html>

4. JavaScript clock and date example


<HTML>
<head><script Language="JavaScript">
<!-- Hide me
function gettheDate()
{
Todays = new Date();
TheDate = "" + (Todays.getMonth()+ 1) +" / "+ Todays.getDate() + " / " +
Todays.getYear();
document.clock.thedate.value = TheDate;
}
var timerID = null;
var timerRunning = false;
function stopclock ()
{
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
function startclock ()
{
stopclock();
gettheDate()
showtime();
}
function showtime ()
{
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" + ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
timeValue += (hours >= 12) ? " P.M." : " A.M."
document.clock.face.value = timeValue;
// you could replace the above with this
// and have a clock on the status bar:
// window.status = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}// end Hide -->
</script><TITLE>Clock and date example</TITLE></HEAD>
<BODY BACKGROUND="../images/bnd_wire.jpg" TEXT="#000000" onLoad="startclock()">
<HR><center><font size=6>The javascript clock and date example
<TABLE Border=0><TR><TD><center>
<form name="clock" onSubmit="0">Time<BR>
<input type="text" name="face" size=12 value=""></TD><TD>
<center><BR>Date<BR><input type="text" name="thedate" size=12 value="">
</form></TD></TR></TABLE>
</BODY>
</HTML>

JavaScript Programs on Images


1. JS Program to change height and width of the image
<html>
<head><script type="text/javascript">
function changeSize()
{
document.getElementById("compman").height="250";
document.getElementById("compman").width="300";
}
</script></head>
<body>
<img id="compman" src="compman.gif" width="107" height="98" />
<br /><br />
<input type="button" onclick="changeSize()" value="Change height and width of image">
</body>
</html>

2. JS Program to change src of the image


<html>
<head>
<script type="text/javascript">
function changeSrc()
{
document.getElementById("myImage").src="hackanm.gif";
}
</script></head>
<body>
<img id="myImage" src="compman.gif" width="107" height="98" />
<br /><br />
<input type="button" onclick="changeSrc()" value="Change image">
</body>
</html>

3. JS Program to identify the position values on the image or document on mouse


move.
<html>
<head>
<script language = "javascript">
function disp()
{
c.innerText = event.offsetX + ";" + event.offsetY;
}
function print()
{
window.alert("MOUSE IS ON THE IMAGE");
}
</script></head>
<body onmousemove = "disp()">
<img src = "na.bmp" width = 500 height = 500 onmouseover = "print()"><br>
<font size = 6>Mouse Moving on image or document at <h3 id = c></h3>
</body>
</html>

# JavaScript program for table of factorials.


<HTML>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
document.write("<center><h2>Table of Factorials</h2>");
for(i = 1, fact = 1; i < 51; i++, fact *= i) {
document.write(i + "! = " + fact);
document.write("<br>");
}
</SCRIPT>
</BODY>
</HTML>

# Program for internal links


<html>
<body>
<a name=”first”>
This is the first anchor
<ol>
<li>Sunday
<li>Monday
<li>Tuesday
<li>Wednesday
<li>Thursday
<li>Friday
<li>Saturday
</ol>
<a name = “second”>
This is the second anchor
<ul>
<li>IceCream
<li>Meals
<li>Tiffin
<li>Movie
<li>Games
<li>Swimming
</ul>
<a href=”#first”>Return to the First Anchor</a><br>
<a href=”#second”>Return to the Second Anchor</a><br>
</body>
</html>
# BLINKING OF TITLES
<script language=javascript>
function titlebar(val)
{
var msg = "Your message here --- hscripts.com";
var speed = 500;
var pos = val;

var msg1 = " ****** "+msg+" ******";


var msg2 = " ------- "+msg+" -------";

if(pos == 0){
masg = msg1;
pos = 1;
}
else if(pos == 1){
masg = msg2;
pos = 0;
}

document.title = masg;
timer = window.setTimeout("titlebar("+pos+")",speed);
}
titlebar(0);
</script>

Other JAVASCRIPT programs

1. Program to assign value to text field and display.


<html>
<head>
<title>Assigning Value on the Fly to a TextField</title>
</head>
<body bgcolor="aquamarine">
<font face=arial size="+1">
<form name="form1">
Enter your name:
<input type="text" name="yourname" size=60>
<p>Click in the box
<input type="text" name="message" size=60
onClick="this.value='Greetings and Salutations, '+document.form1.yourname.value+ '!';">
<p><input type="reset"></form>
</body>
</html>

2. Program for verifying a name.


<html>
<head>
<title>Verifying a Name</title>
<script language="JavaScript">
function validate(form)
{
if(alpha(form.first) == false){
alert ("First name is invalid");
return false;
}
if(alpha(form.last) == false){
alert("Last name is invalid");
return false;
}
return true;
}
function alpha(textField )
{
if( textField.value.length != 0){
for (var i = 0; i < textField.value.length;i++)
{
var ch= textField.value.charAt(i);
if((ch < "A" || ch > "Z") && (ch< "a" || ch >"z"))
{
return false;
}
}
}
else {
return true;
}
}
</script>
</head>
<body bgcolor="lightgreen">
<font face=verdana><b>
<form name="alphachk" onSubmit="return validate(this);">
Enter your first name:<br>
<input name="first" type="text" size=60>
<p>Enter your last name:<br>
<input name="last" type="text" size=60><p>
<input type=submit value="Check it out">
<input type=reset></form>
</body>
</html>

3. Program to verify email ID.


<html>
<head><title>Checking Email</title>
<script language="JavaScript">
function email(form)
{
if(form.address.value.indexOf("@") != -1 && form.address.value.indexOf(".") != -1)
{
alert("OK address!");
return true;
}
else {
alert("Invalid address");
return false;
}
}
</script>
</head>
<body bgcolor="lightgreen">
<font face=verdana><b><center>
<form name="mailchk" action="/cgi-bin/ml.pl" method="post" onSubmit="return
email(this);">
Enter your email address:<p>
<input name="address" type="text" size=60><p>
<input type=submit value="Check it out">
<input type=reset>
</form></center>
</body>
</html>

4. Program to verify a password.


<html>
<head><title>Verifying a Password</title>
<script language="JavaScript">
function valid(form)
{
if( form.pass.value.length == 0 ){
alert("Please enter a password");
return false;
}
if( form.pass.value.length < 6 ){
alert("Password must be at least 6 characters");
return false;
}
for (var i = 0; i < form.pass.value.length;i++){
var ch= form.pass.value.charAt(i);
if((ch < "A" || ch > "Z") && (ch< "a" || ch >"z")
&& (ch < "0" || ch > "9")){
alert("Password contains illegal characters");
return false;
}
}
alert("OK Password");
return true;
}
</script></head>
<body bgcolor="red">
<font face=verdana><b><center>
<form name="passchk" onSubmit="return valid(this);">
Enter your password:<br>
<input name="pass" type="password" size=33><p>
<input type=submit value="Submit Password">
<input type=reset></form>
</center>
</body>
</html>

You might also like