This repository contains a complete example of an HTML form with client‑side validation using JavaScript, basic styling using CSS, and additional layout support through Bootstrap 5 (via CDN).
The goal of this project is to provide a practical reference for students or beginners who want to learn how to:
- Structure forms in HTML
- Implement custom JavaScript validation
- Organize files into HTML/CSS/JS folders
- Display dynamic error messages to the user
- Prevent form submission when fields are invalid
.
├── index.html
└── form/
├── form.css
└── form.js
- index.html — main page containing the form
- form.css — custom styles for layout and appearance
- form.js — all validation logic
You can simply double‑click the file:
index.html
Or, for a more realistic environment, serve it with a local static server:
python -m http.server 5500Then open in your browser:
http://localhost:5500
The form collects:
- Full Name (required)
- Email (required + validated with regex)
- Age (required + must be between 1 and 99)
- Recommendation option — Yes / No / Maybe (required)
- Languages & Frameworks you know — multiple choice (at least one required)
- Comments or suggestions (optional)
All validation happens before the form is submitted.
| Field | Rule |
|---|---|
| Full Name | Cannot be empty |
| Cannot be empty + must match a valid format | |
| Age | Cannot be empty + must be between 1 and 99 |
| Recommendation | At least one radio option must be selected |
| Languages | At least one checkbox must be selected |
Examples of validation error messages:
- "Name is required."
- "Invalid email."
- "Invalid age (must be between 1 and 99)."
- "Please select an option."
- "Select at least one language or framework."
A valid form submission might look like:
- Full Name:
Ana Silva - Email:
ana.silva@example.com - Age:
24 - Recommendation:
Yes - Known languages/frameworks:
Python,React - Comments:
Great form example!
Then click Submit.
Currently, the form includes:
<form action="/action_page.php" ...>Because this PHP file does not exist in the repository, submitting the form will result in a 404 error unless you add a backend.
Replace action with #:
<form action="#" name="myForm" onsubmit="return validateForm()" method="post">This keeps validation but prevents the form from leaving the page.
Create action_page.php or any other endpoint (PHP/Node/Python/etc.) to handle the form POST request.
Example minimal PHP handler:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['f_name'] ?? '';
$email = $_POST['mail'] ?? '';
$age = $_POST['age'] ?? '';
echo "Form received successfully!";
}
?>Even with client‑side validation, always validate on the server as well.
- Error messages appear dynamically near each invalid field.
- Submission is blocked if any field fails validation (
return false). - JavaScript checks all inputs before allowing form submission.
Feel free to modify:
form/form.css— to change layout, colors, spacingform/form.js— to add new validation rulesindex.html— to add more fields or structure
If you want, I can also help you:
✔ Improve the design using modern Bootstrap components
✔ Create a minimal backend (PHP/Node.js)
✔ Add success messages after submitting
✔ Implement form submission using AJAX
✔ Add accessibility improvements (ARIA guidelines)
This project is free for personal, academic, or educational use.
Feel free to modify or expand it as needed.