Nitesh Tripathi Dsa Assignment 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Name – Nitesh Tripathi

Roll No. – 23254


Course - MCA -1 ‘B’

Q.8 Write a JavaScript program to validate email id using regular expression. Program code:
Source Code:

<html>
<body>
<h3>Using the <i> Regular expression </i> to validate email in JavaScript </h3>
<div id = "output"> </div>
<button onclick = "validateEmail()"> Validate any email </button>
<script>
var output = document.getElementById('output');
function validateEmail() {
let userEmail = prompt("Enter your email.", "[email protected]");
let regex = /^[a-z0-9]+@[a-z]+\.[a-z]{2,3}$/;
let result = regex.test(userEmail);
if (result) {
output
.innerHTML = "The " + userEmail + " is a valid email address!";
} else {
output.innerHTML = "The " + userEmail + " is not a valid email address!";
}
}
</script>
</body>
</html>
Output :
Name – Nitesh Tripathi
Roll No. – 23254
Course - MCA -1 ‘B’

Q.9 Write a JavaScript program to validate Aadhar card using regular expression.
Source code:
<html>
<head>
</head>
<body>
<p>Name : Nitesh Tripathi Roll no: 23254 </p>
<form>
Aadhaar Number:
<input type="text" id="txtAadhaar" />
<span id="lblError" class="error"></span>
<hr/>
<input type="button" id="btnSubmit" value="Submit" onclick="ValidateAadhaar()"/>
</form>
</body>
</html>
<script>
function ValidateAadhaar() {
var aadhaar = document.getElementById("txtAadhaar").value;
var lblError = document.getElementById("lblError");
lblError.innerHTML = "";
var expr = /^([0-9]{4}[0-9]{4}[0-9]{4}$)|([0-9]{4}\s[0-9]{4}\s[0-9]{4}$)|([0-9]{4}-[0-
9]{4}-[0-9]{4}$)/;
if (!expr.test(aadhaar)) {
lblError.innerHTML = "Invalid Aadhaar Number";
}
else
{
lblError.innerHTML = "Valid Aadhaar Number";
}
}
</script>

Output :
Name – Nitesh Tripathi
Roll No. – 23254
Course - MCA -1 ‘B’

Q.10 Write a JavaScript program to validate password in login form.

Source code:
<html>
<head>
<title> Login Form Validation</title>
<script>
var attempt = 3;
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if ( username == "tripathin750" && password == "Tri@12"){
alert ("Login successfully");
return true;
}
else{
attempt --;
alert("You have left "+attempt+" attempt;");
if( attempt == 0){
document.getElementById("username").disabled = true;
document.getElementById("password").disabled = true;
document.getElementById("submit").disabled = true;
return false;
}
}
}
</script>
</head>
<body>
<form id="form_id" method="post" name="myform">
<label>User Name :</label>
<input type="text" name="username" id="username"/>
<label>Password :</label>
<input type="password" name="password" id="password"/>
<input type="button" value="Login" id="submit" onclick="validate()"/>
</form>
</body>
</html>

Output :
Name – Nitesh Tripathi
Roll No. – 23254
Course - MCA -1 ‘B’

Q.11 Write a JavaScript to validate the basic registration form.

Source code:
<!DOCTYPE html>
<html>
<head>
<title>Registration Form Validation</title>
<script type="text/javascript">
function validateForm() {
var name = document.forms["registrationForm"]["name"].value;
var email = document.forms["registrationForm"]["email"].value;
var password = document.forms["registrationForm"]["password"].value;

// Regular expressions for email and password validation


var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var passwordPattern = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}$/;

if (name === "") {


alert("Name must be filled out");
return false;
}

if (email === "") {


alert("Email must be filled out");
return false;
} else if (!email.match(emailPattern)) {
alert("Invalid email address");
return false;
}

if (password === "") {


alert("Password must be filled out");
return false;
} else if (!password.match(passwordPattern)) {
alert("Password must contain at least 6 characters, including at least one number, one lowercase,
and one uppercase letter");
return false;
}
}
</script>
</head>
<body>
<h2>Registration Form</h2>
<form name="registrationForm" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="name"><br><br>
Email: <input type="text" name="email"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Output :
Name – Nitesh Tripathi
Roll No. – 23254
Course - MCA -1 ‘B’

Q.12 Write a JavaScript program to create a clock and display time in each sec.

Source code:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Clock</title>
</head>
<body>
<h2>JavaScript Clock</h2>
<p id="demo"></p>
<script>
function displayTime() {
let date = new Date();
let time = date.toLocaleTimeString();
document.getElementById('demo').textContent = time;
}
const createClock = setInterval(displayTime, 1000);
</script>
</body>
</html>

Output :
Name – Nitesh Tripathi
Roll No. – 23254
Course - MCA -1 ‘B’

Q.13 Write a JavaScript program to draw rectangle shape.

Source code:

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="500" height="500" style="border:1px solid #d3d3d3;">


Your browser does not support the HTML5 canvas tag.
</canvas>

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.rect(20, 20, 150, 100);
ctx.stroke();
</script>

</body>
</html>

Output :
Name – Nitesh Tripathi
Roll No. – 23254
Course - MCA -1 ‘B’

Q.14 Write a JavaScript to draw 2 intersecting rectangles.


Source code:

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="500" height="500" style="border:1px solid #d3d3d3;">


Your browser does not support the HTML5 canvas tag.
</canvas>

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

// Draw the first rectangle


ctx.rect(50, 50, 200, 100);
ctx.stroke();

// Draw the second rectangle


ctx.rect(100, 80, 200, 100);
ctx.stroke();
</script>

</body>
</html>

Output :

You might also like