<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Form Validation</title>
<style>
.error-message {
color: red;
font-size: 0.8em;
margin-top: 5px;
}
</style>
</head>
<body>
<h2>JavaScript Form Validation Example</h2>
<form id="myForm">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<span class="error-message" id="nameError"></span>
<br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<span class="error-message" id="emailError"></span>
<br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="18" max="99" required>
<span class="error-message" id="ageError"></span>
<br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<span class="error-message" id="passwordError"></span>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
padding: 20px;
}
h2 {
color: #333;
text-align: center;
}
form {
max-width: 400px;
margin: 0 auto;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
label {
font-weight: bold;
}
input[type="text"],
input[type="email"],
input[type="number"],
input[type="password"] {
width: calc(100% - 20px);
padding: 10px;
margin: 8px 0;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
margin-top: 10px;
font-size: 16px;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.error-message {
color: red;
font-size: 0.8em;
margin-top: 5px;
}
const form = document.getElementById('myForm');
form.addEventListener('submit', function(event) {
// Prevent default form submission
event.preventDefault();
// Perform validation
if (validateForm()) {
// Submit the form
alert('Form submitted successfully!');
form.reset(); // Optional: Reset the form after submission
}
});
function validateForm() {
let isValid = true;
// Validate name
const name = document.getElementById('name').value.trim();
const nameError = document.getElementById('nameError');
if (name === '') {
nameError.textContent = 'Name is required';
isValid = false;
} else if (name.length < 3 || name.length > 16) {
nameError.textContent = 'Name is to short ';
isValid = false;
} else {
nameError.textContent = '';
}
// Validate email
const email = document.getElementById('email').value;
const emailError = document.getElementById('emailError');
if (!isValidEmail(email)) {
emailError.textContent = 'Please enter a valid email address';
isValid = false;
} else {
emailError.textContent = '';
}
// Validate age
const age = document.getElementById('age').value;
const ageError = document.getElementById('ageError');
if (age < 18 || age > 99) {
ageError.textContent = 'Age must be between 18 and 99';
isValid = false;
} else {
ageError.textContent = '';
}
// Validate password
const password = document.getElementById('password').value;
const passwordError = document.getElementById('passwordError');
if (password.length < 8) {
passwordError.textContent = 'Password must be at least 8 characters long';
isValid = false;
} else {
passwordError.textContent = '';
}
return isValid;
}
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.