Nitesh Tripathi Dsa Assignment 2
Nitesh Tripathi Dsa Assignment 2
Nitesh Tripathi Dsa Assignment 2
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’
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’
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;
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’
Source code:
<!DOCTYPE html>
<html>
<body>
<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’
<!DOCTYPE html>
<html>
<body>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
</body>
</html>
Output :