Java Script
Loop, Function ,Validation, Regular Expression
Example:1
<!doctype html>
<html>
<head>
<title> Java script-Loop</title>
</head>
<body>
<script>
for(i=0;i<=12;i++)
{
if(i==5)
{
x=i;
continue;
}
document.write(i);
document.write("</br>");
}
document.write("number" +x+ "is missing in above list");
</script>
</body>
</html>
Example :2
<!doctype html>
<html>
<head>
<title> Java script-Switch-Case</title>
</head>
<body>
<script>
var d=new Date();
ch=d.getMonth();
//var m=1;
switch(ch)
{
case 1:document.write("jan");
break;
case 2:document.write("feb")
break;
case 3:document.write("mar")
break;
case 4:document.write("apr")
break;
case 5:document.write("may")
break;
case 6:document.write("june")
break;
case 7:document.write("july")
break;
case 8:document.write("aug")
break;
case 9:document.write("sep")
break;
case 10:document.write("oct")
break;
case 11:document.write("nov")
break;
case 12:document.write("dec")
break;
}
</script>
</body>
</html>
Example 3:
<!doctype html>
<html>
<head>
<title> Java script-Event</title>
<script>
function check()
{
var eno,name;
eno=document.getElementById("eno").value;
name=document.getElementById("name").value;
document.write("eno"+eno+ "name"+name+"");
alert("Enrollment No." + eno+ "Name"+name+"");
}
</script>
</head>
<body>
<form name="myfrm">
<label>eno</label>
<input type="text" name="eno" id="eno">
<label>name</label>
<input type="text" name="name" id="name">
<input type="submit" id="submit" onclick="check()">
</form>
</body>
</html>
Example 4:
<!doctype html>
<html>
<head>
<title> Java script-Function</title>
<script type="text/javascript">
var x=mul(10,20);
document.write("</br>answer:" +x);
function mul(x,y)
{
document.write("x:=" +x);
document.write("y:=" +y);
return x*y;
}
</script>
</head>
</body>
</html>
Example 5:
<!doctype html>
<html>
<head>
<title> Java script-Validation</title>
<script type="text/javascript">
function chk(eno1,pass1)
{
//var eno=document.f1.eno.value;
// var pass=document.f1.pass.value;
var eno1=document.getElementById("eno").value;
var pass1=document.getElementById("pass").value;
if(eno1=="163" || eno1=="160")
{
alert("enter proper eno");
return false;
}
else if(pass1.length>6)
{
alert("Password must be at least 6 characters long.");
return false;
}
return true;
}
</script>
</head>
<body>
<form name="f1" action="#">
<table border="1">
<tr>
<td>Enrollment No.:</td>
<td><input type="text" size="30" name="eno"></td>
</tr>
<tr>
<td>Password.:</td>
<td><input type="password" size="30" name="pass"></td>
</tr>
<tr rowspan="3">
<td><input type="submit" value="check" onclick="chk()"></td>
</tr>
</table>
</form>
</body>
</html>
Example 6:
<!DOCTYPE html>
<html>
<head>
<title> Java script-Regular Expression/title>
<script type="text/javascript">
function test_str() {
var res;
var str =
document.getElementById("t1").value;
if (str.match(/[a-z]/g) && str.match(
/[A-Z]/g) && str.match(
/[0-9]/g) && str.match(
/[^a-zA-Z\d]/g) && str.length >= 8)
res = "TRUE";
else
res = "FALSE";
document.getElementById("t2").value = res;
}
</script>
</head>
<body oncontextmenu="return false" onLoad="disableClick()">
<p>
String:
<input type="text" placeholder="abc" id="t1" />
<br/>
<br/>
<input type="button"
value="Check"
onclick="test_str()" />
<br/>
<br/> Output:
<input type="text" id="t2" readonly/>
</p>
</body>
</html>
Example 7:
<!doctype html>
<html>
<head>
<title> Java script-Validation</title>
<script>
function validemail()
{
var x=document.myfrm.email.value;
var atposition=x.indexOf("@");
var dotposition=x.lastIndexOf(".");
if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length)
{
alert("Please enter a valid e-mail address \n atpostion:"+atposition+"\n dotposition:"+dotposition);
return false;
}
}
</script>
<body>
<form name="myfrm" method="post" action="#" onsubmit="return validemail();">
Email: <input type="text" name="email"><br/>
<input type="submit" value="register">
</form>
</body>
</html>
Example 8:
<!doctype html>
<html>
<head>
<title> Java script-Validation</title>
<script>
function w(f1)
{
var pass=document.f1.pass.value;
if(pass.length ===0)
{
alert("enter your password 3 char long");
return false;
}
return true;
}
</script>
</head>
<body>
<form name="f1">
Password<input type="text" name="pass" size="30">
<input type="submit" name="submit" onclick="w()">
</form>
</body>
</html>
Example 9:
<!doctype html>
<html>
<head>
<title> Java script-Dialog box</title>
<script type="text/javascript">
function check()
{
alert("hello world");
}
</script>
</head>
<body>
<script type="text/javascript">
check();
</script>
</body>
</html>
Example 10:
<html>
<head>
<title> Java script-Function</title>
<script>
function sayHello(name, age) {
document.write (name + " is " + age + " years old.");
}
</script>
</head>
<body>
<form name="myfrm">
<input type = "button" onclick = "sayHello('James', 33)" value = "Say Hello">
</form>
</body>
</html>