Skip to content

tdlopes95/html-form-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

📄 HTML Form with JavaScript Validation

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

📁 Project Structure

.
├── 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

🚀 Demo & Usage

✔ 1. Open the project locally

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 5500

Then open in your browser:

http://localhost:5500

✔ 2. Filling out the form

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)

✔ 3. Built‑in validation (JavaScript)

All validation happens before the form is submitted.

Field Rule
Full Name Cannot be empty
Email 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."

📌 Usage Example

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.


⚠ About action="/action_page.php"

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.

You have two options:

➤ A) For learning/testing only (recommended for this repo)

Replace action with #:

<form action="#" name="myForm" onsubmit="return validateForm()" method="post">

This keeps validation but prevents the form from leaving the page.


➤ B) Integrate with a real backend

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.


🧪 Testing & Debugging

  • 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.

🎨 Customization

Feel free to modify:

  • form/form.css — to change layout, colors, spacing
  • form/form.js — to add new validation rules
  • index.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)


📜 License

This project is free for personal, academic, or educational use.
Feel free to modify or expand it as needed.

About

A simple and beginner‑friendly example demonstrating how to build and style an HTML form. This project shows the essential structure and elements needed to collect user input on a webpage, including text fields, email inputs, dropdowns, checkboxes, radio buttons, and a submit button.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors