- Click below to download Unit 2 programs in pdf file: -
UNIT 2
<!-- 1. Write a javascript to print your name and suname on screen. -->
<script>
var name, surname
name=prompt("Enter your name : ")
surname=prompt("Enter your surname : ")
document.write("<h1> Hello, "+name+" "+surname)
</script>
<!-- 2. Write a javascript to find sum of two numbers entered by user. -->
<script type="text/javascript">
const num1 = parseInt(prompt("Enter the first number :"));
const num2 = parseInt(prompt('Enter the second number :'));
const sum = num1 + num2;
document.write("The sum of "+num1 +" and "+num2+" is "+sum);
</script>
<!-- 3. Write a javascript to find sum of N numbers entered by user. -->
<html>
<head>
<script>
var i=0, n, sum=0, temp;
function fun()
{
n = parseInt(document.getElementById("n").value);
if(n)
{
document.getElementById("nVal").innerHTML = n;
document.getElementById("tme").innerHTML = i+1;
i++;
temp = document.getElementById("one");
temp.style.display = "none";
temp = document.getElementById("two");
temp.style.display = "block";
}
}
function add()
{
if(i==n)
{
temp = document.getElementById("two");
temp.style.display = "none";
temp = document.getElementById("three");
temp.style.display = "block";
}
num = parseInt(document.getElementById("num").value);
if(num)
{
sum = sum + num;
document.getElementById("num").value = null;
document.getElementById("tme").innerHTML = i+1;
i++;
}
}
function res()
{
document.getElementById("result").value = sum;
}
</script>
</head>
<body>
<p id="one">Enter the Value of n: <input id="n">
<button onclick="fun()">ENTER</button></p>
<p id="two" style="display:none;">Enter <span id="nVal"></span>
Numbers. Input No. <span id="tme"></span>:
<input id="num"><button id="btn" onclick="add()">ENTER</button></p>
<p id="three" style="display:none;"><button onclick="res()">Add</button>
<input id="result"></p>
</body>
</html>
<!-- 4. Write javascript to validate textbox to accept only text value -->
<html>
<head>
<title>validate textbox to accept only text value</title>
<script language="Javascript" type="text/javascript">
function allowOnlyLetters(e, t)
{
if (window.event)
{
var charCode = window.event.keyCode;
}
else if (e)
{
var charCode = e.which;
}
else { return true; }
if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
return true;
else
{
alert("Please enter only alphabets");
return false;
}
}
</script>
</head>
<body>
<table align="center">
<tr>
<td>
<b>Enter your input :<input type="text" onkeypress="return allowOnlyLetters(event,this);" /> </b>
</td>
</tr>
</table>
</body>
</html>
<!-- 5 Write a javascript to find reverse of given string. -->
<script type="text/javascript">
function reverseString(str)
{
let newString = "";
for (let i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
}
const string = prompt('Enter a string: ');
const result = reverseString(string);
document.write(result);
</script>
<!-- 6. Write a java script to validate email address entered in textbox -->
<html>
<head>
<script>
function checkEmail() {
var email = document.getElementById('txtEmail');
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
}
</script>
</head>
<body>
<p id="one">Enter the e-mail : <input id="txtEmail">
<button id="btn" onclick="checkEmail()">Check</button>
</body>
</html>
0 Comments