WEB LAB MSE-1 PROGRAM 7-10 AND PHP PROGRAMS:
7.) Write JavaScript to validate the following fields of the Registration form.
i) Name (field can not be empty)
ii) Age (Number validation)
iii) Password
iv) Confirm Password
Program:
<html>
<head>
<title> Validate the Password </title>
</head>
<script>
function validateForm() {
var pw1 = document.getElementById("pswd1").value;
var pw2 = document.getElementById("pswd2").value;
var name = document.getElementById("fname").value;
var age = document.getElementById("age").value;
if(name == "") {
alert("Enter the Name");
return false;
}
if(age == "") {
alert("Enter the Age");
return false;
}
else if(isNaN(age)||age>100||age<1){
alert("Enter a valid age");
return false;
}
if(pw1 == "") {
alert("Enter the password");
return false;
}
if(pw1.length < 8||pw1.length > 15) {
alert("Password must be minimum 8 and maximum 15 characters");
return false;
}
if(pw2 == "") {
alert("Enter the password");
return false;
}
if(pw1 != pw2) {
alert("Password does not match");
return false;
}
else {
alert ("Your password created successfully");
}
}
</script>
<body>
<form onsubmit ="return validateForm()"><b>
<fieldset>
<legend>Student Information Form</legend><br>
Name : <input type="text" id="fname"><br><br>
Age : <input type="number" id="age"><br><br>
Password : <input type = "password" id="pswd1"><br><br>
Confirm Password : <input type="password" id="pswd2"><br><br>
<input type="submit" value="Submit">
<button type="reset" value="Reset" >Reset</button>
</form>
</body>
</html>
Output:
8.) Write JavaScript to validate the following fields of the Registration form.
i) Name (Name should contains alphabets)
ii)Password (Password should not be less than 6 characters length).
iii) Phone number (Phone number should contain 10 digits only)
Program:
<html>
<head>
<title>Student Information Form</title>
<script type="text/javascript">
function valid()
{
var na = document.getElementById("nm").value;
var pas = document.getElementById("passwd").value;
var mno = document.getElementById("pno").value;
if(na==""||na==null)
{
alert("Enter The Name");
return false;
}
else if(isNaN(pas)||pas.length<6)
{
alert("The password must be minimum 6 and not character");
return false;
}
else if(mno==""||mno==null)
{
alert("Enter The Phone No");
return false;
}
else if(isNaN(mno)||mno.length>10||mno.length<10)
{
alert("The mobile no. always has 10 digit numerical value");
return false;
}
else
alert("The Student Information Submitted Successfully");
}
</script>
</head>
<body>
<form action="" onsubmit="return valid()" method="post"><b>
<fieldset>
<legend>Student Information Form</legend><br>
Name: <input type="text" id="nm"><br><br>
Password: <input type="password" id="passwd"><br><br>
Phone Number: <input type="text" id="pno"><br><br>
<input type="submit" value="Submit">
<button type="reset" value="Reset" >Reset</button>
</fieldset>
</form>
</body>
</html>
Output:
9.)Write JavaScript to validate the following fields of the Faculty Job
Application Form
i) First Name
ii) email id ( email validation)
iii) Address
iv) Mobile number
v) Years of experience
Program:
<html>
<head>
<title>Faculty Information Form</title>
<script type="text/javascript">
function valid()
{
var na = document.getElementById("name").value;
var mail = document.getElementById("email").value;
var addr = document.getElementById("addrs").value;
var mno = document.getElementById("pno").value;
var yearex = document.getElementById("year").value;
if(na==""||na==null)
{
alert("Enter The Name");
return false;
}
else if(mail==""||mail==null)
{
alert("Enter The Email-Id");
return false;
}
else if(addr==""||addr==null)
{
alert("The The Address");
return false;
}
else if(mno==""||mno==null)
{
alert("Enter The Phone No");
return false;
}
else if(isNaN(mno)||mno.length>10||mno.length<10)
{
alert("The mobile no. always has 10 digit numerical value");
return false;
}
else if(isNaN(yearex)||yearex<3)
{
alert("Not Eligible , Experience should be atleast 3 years");
return false;
}
alert("The Faculty Information Submitted Successfully");
}
</script>
</head>
<body>
<form action="" onsubmit="return valid()" method="post"><b>
<fieldset>
<legend>Faculty Information Form</legend><br>
Name : <input type="text" id="name"><br><br>
Email-id : <input type="text" id="email"><br><br>
Address : <input type="text" id="addrs"><br><br>
Phone Number : <input type="text" id="pno"><br><br>
Year-of-Experience : <input type="number" id="year"><br><br>
<input type="submit" value="Submit">
<button type="reset" value="Reset" >Reset</button>
</fieldset>
</form>
</body>
</html>
Output:
10.) Write a JavaScript to design a simple calculator to perform the following
operations: sum, product, difference and quotient.
Program:
<html>
<head>
<title>calculator</title>
<script>
function performop(op)
{
var result=0;
var n1=parseInt(document.getElementById("num1").value);
var n2=parseInt(document.getElementById("num2").value);
if(op=="+"){
var result=n1+n2;
}
else if(op=="-"){
var result=n1-n2;
}
else if(op=="*"){
var result=n1*n2;
}
else{
var result=n1/n2;
}
document.getElementById("res").value=result;
}
</script>
</head>
<body >
<form><b>
<fieldset>
<legend>Calculator</legend><br>
Enter 1st number : <input type="number" name="num1"
id="num1"><br><br>
Enter 2nd number : <input type="number" name="num2"
id="num2"><br><br>
ADD<input type="button" value="+"
onclick="performop(this.value)"/>
SUBSTRACT<input type="button" value="-"
onclick="performop(this.value)"/>
MULTIPLY<input type="button" value="*"
onclick="performop(this.value)"/>
DIVIDE<input type="button" value="/"
onclick="performop(this.value)"/><br><br>
Result<input type="text" name="res" id="res" readonly/>
<button type="reset" value="Reset" >Reset</button>
</fieldset>
</form>
</body>
</html>
Output:
1.) Write a PHP code to Connect to Database, Create a table College (Dname,
Dno, Number_Of_faculty) Insert 3 records of your choice.
Program:
<?php
$servername = "172.16.2.3";
$database = "student";
$username = "student";
$password = "student";
$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "CREATE TABLE college
(
DNo int,
DName varchar(50),
No_of_Faculty int
)";
if ($conn->query($sql) === TRUE) {
echo "Table created successfully";
}
$sql2 = "INSERT INTO college VALUES(1000,'InformationScience',28),
(1001,'ComputerScience',32),(1002,'ArtificialIntelligence',25)";
if (mysqli_query($conn, $sql2))
{
echo "<br>";
echo "New record created successfully !";
echo "<br>";
}
else
{
echo "Error: ";
}
mysqli_close($conn);
?>
2.) Write a PHP code to Connect to Database, Create a table Account (AcctNo,
Name, Branch, Balance) Insert 3 records of your choice.
Program:
<?php
$servername = "172.16.2.3";
$database = "student";
$username = "student";
$password = "student";
$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "CREATE TABLE account
(
AccNo int,
Name varchar(50),
Branch varchar(50),
Balance int
)";
if ($conn->query($sql) === TRUE) {
echo "Table created successfully";
}
$sql2 = "INSERT INTO account VALUES(11101,'Anand','Belman',150000),
(11102,'Ramesh','Karkala',380500),(11103,'Umanath','Nitte',,250000)";
if (mysqli_query($conn, $sql2))
{
echo "<br>";
echo "New record created successfully !";
echo "<br>";
}
else
{
echo "Error: ";
}
mysqli_close($conn);
?>
3.) Write a PHP program to connect to the database. Create a table named
“Student” with following fields (sno, sname, percentage). Insert 3 records of
your choice.
Program:
<?php
$servername = "172.16.2.3";
$database = "student";
$username = "student";
$password = "student";
$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "CREATE TABLE student
(
SNo int,
SName varchar(50),
Percentage int
)";
if ($conn->query($sql) === TRUE) {
echo "Table created successfully";
}
$sql2 = "INSERT INTO student VALUES(4nm22is01,'Mohith',85),
(4nm22is02,'Rakesh',78),(4nm22is03,'Umesh',92)";
if (mysqli_query($conn, $sql2))
{
echo "<br>";
echo "New record created successfully !";
echo "<br>";
}
else
{
echo "Error: ";
}
mysqli_close($conn);
?>
4.) Write a PHP program to connect to the database. Create a table named
“Employee” with following fields (Eno, Ename, Salary). Insert 2 records of your
choice.
Program:
<?php
$servername = "172.16.2.3";
$database = "student";
$username = "student";
$password = "student";
$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "CREATE TABLE employee
(
ENo int,
EName varchar(50),
Salary int
)";
if ($conn->query($sql) === TRUE) {
echo "Table created successfully";
}
$sql2 = "INSERT INTO employee VALUES(1111,'Deleep',150000),
(1112,'Ganesh',140000),(1113,'Vijay',100000)";
if (mysqli_query($conn, $sql2))
{
echo "<br>";
echo "New record created successfully !";
echo "<br>";
}
else
{
echo "Error: ";
}
mysqli_close($conn);
?>