DHTML Unit 2 programs


UNIT - 2 Programs

NOTE: ALL THE PROGRAMS DONE BY SHAKSHI SHAH.........!

1. Write a javascript to print your name and surname on screen.
ANS)

<html>
<head>
<title>UNIT 2 - prg 1</title>
<script>
function check()
{

var first,last;
first = document.getElementById("v1").value;
last = document.getElementById("v2").value;
document.getElementById("ans").innerHTML= first + " "  +last;
}
</script>
</head>

<body>
Name : <br>
<input type="text"  id="v1">
<br><br>

Surname : <br>
<input type="text"  id="v2">
<br><br>

<input type="button" onclick="check()" value="click to print name">
<br><br>

<span id="ans"></span>
<span id="ans1"></span>
</body>
</html>


2. Write a javascript to find sum of two numbers entered by user.
ANS)
<html>
<head>

<title>UNIT 2 - prg 2</title>
<script>
function addtion()
{
first=parseInt(document.getElementById("v1").value);
last =parseInt(document.getElementById("v2").value);
document.getElementById("ans").innerHTML= first + last;
}
</script>
</head>

<body>

<p>Enter number 1</p>
<input type="number"  id="v1">
<br>

<p>Enter number 2 </p>
<input type="number "  id="v2">
<br><br>

<input type="button" onclick="addtion()" value="Addition">
<br><br>
<span id="ans"></span>

</body>
</html>


3. Write a javascript to find sum of N numbers entered by user.
ANS)

<html>
<head>
<script type = "text/javascript">
function addition()
{
var i=0;
var sum=0;
i = parseInt(document.getElementById("v1").value);
for(;i>0;i--)
{
sum=sum+i;
}
document.getElementById("ans").innerHTML=sum;
}
</script>
</head>

<body>
Enter number :
<input type="text" id="v1">
<br><br>
<input type="button" value="Find sum of N numbers" onclick="addition()">
<br><br>
Sum of natural no is =

<span id="ans"></span>

</body>
</html>

4. Write javascript to validate textbox to accept only text value.
ANS)

<html>
<head>
<title>UNIT 2 - prg 4</title>
<script>
function check(event)
{
if(event.which<65 || event.which>122)
{
alert("Plese entered only text");
}

}
</script>
</head>
<body>
Enter any text <br><br>
<input type="text" id="v1" onkeypress="check(event)">
<br><br>
<span id="v2"></span>

</body>
</html>


5 Write a javascript to find reverse of given string.
ANS)

<html>
<head>
<title></title>
<script>
function reverse()
{
var str = document.getElementById("value").value;
var split_str = str.split("");      //to split string into array
var rev_str = split_str.reverse(""); //to reverse the array
var ans = rev_str.join(""); //to join the array into string
document.getElementById("ans").innerHTML=ans; //to print the reverse string
}
</script>
</head>

<body>

<input type="text" id="value" placeholder="Enter String">
<br><br>
<input type="button" value="Click to reverse the string" onclick="reverse()">
<br>
<span id="ans"></span>

</body>
</html>


6. Write a javascript to validate email address entered in textbox.
ANS)

<html>
<head>
<title>Unit 2 - prg 6</title>
<script>
function check()
{
var a = document.getElementById("value").value;

var att_pos = a.indexOf("@");
var dot_pos = a.lastIndexOf(".");
if(att_pos<0 || dot_pos<0)
{

alert("You enter invalid email address...1");
}
if(att_pos == 0 || att_pos > dot_pos)
{
alert("You enter invalid email address...2");
}
if((dot_pos - att_pos) < 2)
{
alert("You enter invald email address...3");
}
if((a.length-1) <= dot_pos)
{
alert("You enter invalid email address...4");
}
}

</script>
</head>
<body>

<input type="text" id="value" placeholder="Enter email address" />
<br>
<input type="button" id="button" onclick="check();" value="Login">

</body>
</html>

Comments

Popular posts from this blog

C++ Practice Program

Cloud Computing Important Question-Answer for University Exam

Software Testing Gujarat University Important Questions For Exam