0% found this document useful (0 votes)
8 views3 pages

Css Exp 12.1 PDF Updated Tuesday

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Css Exp 12.1 PDF Updated Tuesday

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Name: Ansari Abdullah Sohel

Roll No: 220402


Subject: CSS
Practical No: 12
AIM: Develop a webpage for validation of form fields using regular expressions.
1. Write a JavaScript program to validate Name, Age, Date of Birth, Email Id and Password using
regular expression.
Code:
<html>
<head>
<title>Form Validation</title>
</head>
<body>
<br><br>

<form name="myForm">
Name: &nbsp; <input type="text" id="name" onblur="validateName()"><br><br>
Age: &nbsp; <input type="text" id="age" onblur="validateAge()"><br><br>
Date of Birth: &nbsp; <input type="text" id="dob" onblur="validateDOB()"><br><br>
E-mail ID: &nbsp; <input type="text" id="email" onblur="validateEmail()"><br><br>
Password: &nbsp; <input type="text" id="password" onblur="validatePassword()"><br><br>
</form>

<script>
function validateName() {
var regex = /^[a-zA-Z]{2,30}$/;
var name = document.getElementById("name").value;
if (regex.test(name)) {
alert("Successful.");
} else {
alert("Name should not contain special characters or digits.");
}
}

function validateAge() {
var regex = /^(0[1-9]|[1-9][0-9])$/;
var age = document.getElementById("age").value;
if (regex.test(age) && age >= 18) {
alert("Successful.");
} else {
alert("Age should be between 18 and 99.");
}
}

function validateDOB() {
var regex = /^(0[1-9]|[12][0-9]|3[01])[./-](0[1-9]|1[0-2])[./-](19|20)\d\d$/;
var dob = document.getElementById("dob").value;
if (regex.test(dob)) {
alert("Successful.");
} else {
alert("Formats: dd-mm-yyyy, dd/mm/yyyy, dd.mm.yyyy");
}
}
function validateEmail() {
var regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var email = document.getElementById("email").value;
if (regex.test(email)) {
alert("Successful.");
} else {
alert("Invalid E-mail...");
}
}

function validatePassword() {
var regex = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;
var password = document.getElementById("password").value;
if (regex.test(password)) {
alert("Successful.");
} else {
alert("Password must contain at least 1 uppercase, 1 lowercase, 1 digit, 1 special character,
and be between 6-16 characters long.");
}
}
</script>
</body>

</html> Output:

2. Write a JavaScript program to validate the IP Address using regular expression. Code:
Code:
<html>
<head>
<title>Experiment No.12</title>
</head>
<body>
<h1>Ansari Abdullah - 220402</h1>
<form>
<label>IP:</label>
<input type="text" id="myIp" onblur="validateIp()" size="30"/>
</form>

<script>
function validateIp() {
var ip = document.getElementById('myIp').value;
var regex = /^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-
9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;

if (regex.test(ip) == true) {
alert('Successful');
} else {
alert('Invalid IP');
}
}
</script>
</body>
</html>
Output:

You might also like