You are on page 1of 2

226370307068

PRACTICAL NO 8(2)
AIM: Create a web page a form to collect employee information to validate
user information using regular expressions.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Employee Information Form</title>
<script>
function validateForm(){
var name = document.forms["employeeForm"]["name"].value;
var email = document.forms["employeeForm"]["email"].value;
var position = document.forms["employeeForm"]["position"].value;
var salary = document.forms["employeeForm"]["salary"].value;
// Regular expression patterns for validation
var namePattern = /^[a-zA-Z ]+$/;
var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
var salaryPattern = /^[0-9]+(?:\.[0-9]+)?$/;
// Client-side validation
if (!namePattern.test(name)) {
alert("Invalid name format. Only letters and spaces are allowed.");
return false;
}
if (!emailPattern.test(email)) {
alert("Invalid email address.");
return false;
}
if (!salaryPattern.test(salary) || salary <= 0) {
alert("Invalid salary format. Please enter a positive number.");
return false;
226370307068

}
// If all validations pass, the form will be submitted
return true;
}
</script>
</head>
<body>
<h2>Employee Information Form</h2>
<form name="employeeForm" action="process_form.php" method="post"
onsubmit="return
validateForm()">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
<label for="position">Position:</label>
<input type="text" id="position" name="position" required><br>
<label for="salary">Salary:</label>
<input type="text" id="salary" name="salary" required><br>
<input type="submit" value="Submit">
</form></body></html>

OUTPUT:

You might also like