Understanding the basics of HTML forms is essential for web development. Here’s a comprehensive guide on HTML forms, covering the fundamental elements and their attributes.
An HTML form is created using the <form>
element. Forms can include various input elements, such as text fields, checkboxes, radio buttons, and submit buttons.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic HTML Form</title>
</head>
<body>
<form action="/submit" method="post">
<!-- Form Elements Go Here -->
</form>
</body>
</html>
GET
and POST
.<form action="/submit" method="post">
<!-- Form Elements Go Here -->
</form>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="0" max="120">
<label for="dob">Date of Birth:</label>
<input type="date" id="dob" name="dob">
<label for="subscribe">Subscribe to newsletter:</label>
<input type="checkbox" id="subscribe" name="subscribe">
<p>Gender:</p>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
<label for="country">Country:</label>
<select id="country" name="country">
<option value="usa">United States</option>
<option value="canada">Canada</option>
<option value="uk">United Kingdom</option>
</select>
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea>
<button type="submit">Submit</button>
HTML5 offers built-in form validation. Here are some attributes used for validation:
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="userage">Age:</label>
<input type="number" id="userage" name="userage" min="18" max="99" required>
<label for="useremail">Email:</label>
<input type="email" id="useremail" name="useremail" required pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$">
Here’s a complete example form incorporating various elements and attributes:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Example Form</title>
</head>
<body>
<h1>Contact Form</h1>
<form action="/submit" method="post">
<label for="fullname">Full Name:</label>
<input type="text" id="fullname" name="fullname" required>
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<label for="dob">Date of Birth:</label>
<input type="date" id="dob" name="dob">
<br>
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="0" max="120">
<br>
<p>Gender:</p>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
<br>
<label for="country">Country:</label>
<select id="country" name="country">
<option value="usa">United States</option>
<option value="canada">Canada</option>
<option value="uk">United Kingdom</option>
</select>
<br>
<label for="message">Message:</label>
<textarea id="message" name="message"></textarea>
<br>
<label for="subscribe">Subscribe to newsletter:</label>
<input type="checkbox" id="subscribe" name="subscribe">
<br>
<button type="submit">Submit</button>
</form>
</body>
</html>
Understanding and using HTML forms correctly is crucial for gathering user input in web applications. With these basics, you can create a variety of forms to collect and validate data effectively.