Practical ANS
Practical ANS
Number validations:
<html>
<head>
<title>Check Positive, Negative or Zero Number</title>
</head>
<body>
<script>
var no = prompt("Enter the number", "");
no = parseInt(no);
if(no < 0)
document.write("You entered Negative Number.");
else if(no > 0)
document.write("You entered Positive Number.");
else
document.write("You entered Zero.");
</script>
</body>
</html>
(2) Read 2 numbers and perform the Arithmetic operations (+, -, *, /) on them, and
show the output of the operations.
<!DOCTYPE html>
<html>
<body>
<form>
1st Number : <input type="text" id="fn" /><br>
2nd Number: <input type="text" id="sn" /><br>
<script type="text/javascript">
function multiplyBy()
{
num1 = document.getElementById("fn").value;
num2 = document.getElementById("sn").value;
document.getElementById("result").innerHTML = num1 * num2;
}
function divideBy()
{
num1 = document.getElementById("fn").value;
num2 = document.getElementById("sn").value;
document.getElementById("result").innerHTML = num1 / num2;
}
function additionBy()
{
num1 = parseInt(document.getElementById("fn").value);
num2 = parseInt(document.getElementById("sn").value);
document.getElementById("result").innerHTML = num1+= num2; }
function divisionBy()
{
num1 = document.getElementById("fn").value;
num2 = document.getElementById("sn").value;
document.getElementById("result").innerHTML = num1-num2;
}
</script>
</body>
</html>
2. String validations:
<html>
<head>
<title>JavaScript Optimum way to compare strings</title>
</head>
<script>
var str1 = document.getElementById("text1");
var str2 = document.getElementById("text2");
var el_down = document.getElementById("GFG_DOWN");
function gfg_Run() {
var a = str1.value;
var b = str2.value;
var ans = a.localeCompare(b);
var res = "";
if(ans == -1) {
res = '"' + a + '" comes before "' + b + '"';
} else if(ans == 0) {
res = 'Both string are same';
} else {
res = '"' + a + '" comes after "' + b + '"';
}
el_down.innerHTML = res;
}
</script>
</body>
</html>
(3) Read a character and check whether it is a Vowel or not.
<html>
<body>
<script>
var input = prompt("Enter a character:");
if (input == 'a' || input == 'A' || input == 'e' || input == 'E' || input == 'i' || input
== 'I' || input == 'o' || input == 'O' || input == 'u' || input == 'U') {
document.write("Its a vowel");
} else if ((input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z')) {
document.write("Its a consonant");
} else {
document.write("Invalid input please enter an alphabet");
}
</script>
</body>
</html>
(4) Read a character and print Male or Female based on M or F and otherwise
‘Invalid Character’.
<html>
<body>
Enter A String ::
<input type="text" id="n1">
<br>
<input type="button" onclick="gender()" value="check">
<script>
function gender()
{
var n1=document.getElementById('n1').value;
if(n1 == "M" ) {
alert("Male");
}
else if(n1 == "F") {
alert("Female");
}
else {
alert("Invalid Character");
}
}
</script>
</body>
</html>
(5) Write a program to check Uid and Password are X and Y (X and Y will be fixed
strings of your choice)
<html>
<body>
<table>
<form>
<tr>
<td>User Name:</td>
<td><Input Type="text" id="n1"></td>
</tr>
<tr>
<td>Password:</td>
<td><Input Type="password" id="n2">
</td>
</tr>
<tr>
<td></td>
<td><button onclick="uid();">Log in</button>
</td>
</tr>
</form>
</table>
<script>
function uid()
{
var user=document.getElementById('n1').value;
var password=document.getElementById('n2').value;
if(user=="admin" || user=="FY12")
{
alert("Logged In");
}
else
{
alert("user name invalid");
}
if(password=="admin" || password=="FY12")
{
alert("Valid");
}
else
{
alert("passwd is invalid");
}
}
</script>
</body>
</html>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>
print current day and time
</title>
</head>
<body>
<script type="text/javascript">
var myDate = new Date();
var myDay = myDate.getDay();
// Array of days.
var weekday = ['Sunday', 'Monday', 'Tuesday',
'Wednesday', 'Thursday', 'Friday', 'Saturday'
];
document.write("Today is : " + weekday[myDay]);
document.write("<br/>");
(2) Read user date and print whether it is past date or future date.
<!DOCTYPE HTML>
<html>
<head>
<title>
How to Check Input Date is Equal
to Today’s Date or not using
JavaScript?
</title>
<style>
body {
text-align: center;
}
h1{
color: green;
}
#geeks {
color: green;
font-size: 29px;
font-weight: bold;
}
</style>
</head>
<body >
<h1>H.P.A.K.S</h1>
<b>
Type the date in given format
and <br>check if it is same as
today's date or not.
</b>
<br><br>
<script>
var down = document.getElementById('geeks');
function gfg() {
var date =
document.getElementById('date').value;
if(inpDate.setHours(0, 0, 0, 0) ==
currDate.setHours(0, 0, 0, 0))
{
down.innerHTML =
"The input date is today's date";
}
else {
down.innerHTML = "The input date is Past"
}
}
</script>
</body>
</html>
4.Forms:
(1) Create a radio control group, read user selection and display appropriate
message.
<html>
<body>
<script type="text/javascript">
function radio()
{
var n1=document.getElementById("n1");
var n2=document.getElementById("n2");
if(n1.checked)
{
alert("Male Button Is Clicked");
}
if(n2.checked)
{
alert("Female Button Is Clicked");
}
}
</script>
Gender
<input type="radio" id="n1" name="radio" onclick="radio()">
<b>Male</b>
<input type="radio" id="n2" name="radio" onclick="radio()">
<b>Female</b>
</body>
</html>
2. Create a checkbox control, read user selection and display appropriate
message.
<!DOCTYPE html>
<html>
<body>
<label>Check Which you have</label><br/>
<input type="checkbox" id="checkbox1" name="bike" value="Bike">
Bike <br/>
<input type="checkbox" id="checkbox2" name="car" value="Car"> Car
<br/>
3. Create a button control, when user clicks it, display/change its label.
<html>
<head>
<script language="javascript">
function abc()
{
document.getElementById("d").innerHTML="You Click a Button";
}
</script>
<body>
<form>
<button type=button id="d" onclick="abc()">Click me??</button>
</form>
</body>
</html>
4. Create a dropdown control, read user selection and display appropriate
message.
<html>
<head>
<title>dropdown menu using select tab</title>
</head>
<script>
function favLanguage() {
var mylist = document.getElementById("myList");
document.getElementById("favourite").value =
mylist.options[mylist.selectedIndex].text;
}
</script>
<body>
<form>
<b> Select you favourite tutorial site using dropdown list </b>
<select id = "myList" onchange = " favLanguage ()" >
<option> ---Choose tutorial--- </option>
<option> JavaScript</option>
<option> Java </option>
<option> Angular </option>
<option> HTML </option>
</select>
<p> Your selected tutorial site is:
<input type = "text" id = "favourite" size = "20" </p>
</form>
</body>
</html>
5. Display an alert box giving message “Button was pressed” using ONCLICK
button event.
<html>
<head>
<title>Forms collection</title>
<script>
function clickEvent()
{
6. Create “CLICK HERE TO ENTER NAME” button using on click event to accept
user name and print it with “Hello! How are you nnnn”. Where nnnn is user
entered name.
<html>
<head>
<title>Forms collection</title>
<script>
function clickEvent()
{
var a=prompt("enter your name here");
document.write("Hello! How are you,"+a);
}
</script>
</head>
<body>
<input type="Submit" value="CLICK HERE TO ENTER NAME"
onclick="clickEvent()">
<h1>hello</h1>
</body>
</html>
7. Display a prompt box giving message “Enter your name” using on load event.
Find the length of the inputted string and display the message “Length of
your name is nn” where nn is the length of the string.
<html>
<head>
<title>Forms collection</title>
<script>
function clickEvent()
{
var a=prompt("enter your name here");
document.write("Hello! How are you,"+a.length);
}
</script>
</head>
<body>
<input type="Submit" value="CLICK HERE TO ENTER NAME"
onclick="clickEvent()">
<h1>hello</h1>
</body>
</html>
8. Write an html page to find the month name from the inputted month number.
Generate an error message if the input is not an integer number.
<html>
<head>
<title>find the month</title>
<script language="Javascript">
function month()
{
var a;
a=(d.t1.value);
if(a==1)
{
d.t2.value="January";
}
else if(a==2)
{
d.t2.value="February";
}
else if(a==3)
{
d.t2.value="March";
}
else if(a==4)
{
d.t2.value="april";
}
else if(a==5)
{
d.t2.value="May";
}
else if(a==6)
{
d.t2.value="June";
}
else if(a==7)
{
d.t2.value="July";
}
else if(a==8)
{
d.t2.value="August";
}
else if(a==9)
{
d.t2.value="september";
}
else if(a==10)
{
d.t2.value="october";
}
else if(a==11)
{
d.t2.value="november";
}
else if(a==12)
{
d.t2.value="December";
}
else
{
alert=("you enter wrong");
}
}
</script>
</head>
<body>
<form name="d">
<table border="2">
<tr>
<td>Month no</td>
<td><input type="text"name="t1"></td>
</tr>
<tr>
<td>Month name</td>
<td><input type="text"name="t2"></td>
</tr>
<tr>
<td colspan="2"align="center">
<input type="button"name="btn"value="show month
name"onclick="month()">
<input type="reset"name="btn2"value="reset">
</td>
</tr>
</table>
</form>
</body>
</html>
9. Write an html page to print result according to selected choice (ADD, SUB,
MUL,DIV). Generate an error message if the inputs are not numbers.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function multiply(){
a=Number(document.my_cal.first.value);
b=Number(document.my_cal.second.value);
c=a*b;
document.my_cal.total.value=c;
}
function addition(){
a=Number(document.my_cal.first.value);
b=Number(document.my_cal.second.value);
c=a+b;
document.my_cal.total.value=c;
}
function subtraction(){
a=Number(document.my_cal.first.value);
b=Number(document.my_cal.second.value);
c=a-b;
document.my_cal.total.value=c;
}
function division(){
a=Number(document.my_cal.first.value);
b=Number(document.my_cal.second.value);
c=a/b;
document.my_cal.total.value=c;
}
function modulus(){
a=Number(document.my_cal.first.value);
b=Number(document.my_cal.second.value);
c=a%b;
document.my_cal.total.value=c;
}
</script>
<br>
</tr>
<tr><td>
<!-- Here user will enter 2nd number. -->
Number 2: <input type="text" name="second">
<br>
</td></tr>
<tr>
<td align="center">
<input type="button" value="ADD" onclick="javascript:addition();">
<input type="button" value="SUB" onclick="javascript:subtraction();">
<input type="button" value="MUL" onclick="javascript:multiply();">
<input type="button" value="DIV" onclick="javascript:division();">
<input type="button" value="MOD"
onclick="javascript:modulus();"></td></tr>
<br><br>
<tr><td>
<!-- Here result will be displayed. -->
Get Result: <input type="text" name="total">
</td></tr>
</table>
</form>
</body>
</html>
10. Create the following Form and perform the following validations. If there is
anything wrong with either of the inputs then generate an error message:
Validations:
a. Both fields are compulsory & it should not contain blank space.
b. User name must contain only characters.
c. Password begins with an alphabet only.
d. Length of password is minimum 8 characters and maximum 16
characters.
11. Calculate Total marks, percentage and class of the student. Directly enter
your name in the textbox. The marks must be in the range of 0-100 only.
Generate an error message if the marks entered are incorrect. (Total,
Percentage, Class are readonly text controls)
Note:
35 – 50% Pass Class, 51 – 75% Second Class, >75% First Class
<html>
<head>
<title>Student Results</title>
<script>
function getReport(){
//find the grade and result using the ternary operator inside the switch
statement
switch(
{
case 1 :grade = "A";result="First Class";break;
case 2 :grade = "B"; result="Second Class";break;
case 3 :grade = "C"; result="Third Class";break;
case 0 :grade = "D"; result="Fail";break;
}
</head>
<body>