Creating a Functional Form with Validation
Course Title: HTML & Web Development Fundamentals: Building Modern Websites Section Title: Forms and User Input Topic: Create a functional form with various input fields and basic validation.(Lab topic)
In this lab, we'll apply the knowledge we've gained from the previous topics to create a functional form with various input fields and basic validation. We'll explore how to use different input types, labels, and validation attributes to create a user-friendly and accessible form.
Objective:
By the end of this lab, you'll be able to:
- Create a functional form with multiple input fields
- Use different input types (e.g., text, email, password, radio buttons, checkboxes, and dropdowns)
- Add labels and fieldsets to improve accessibility and structure
- Implement basic validation using required fields, input patterns, and validation attributes
Step 1: Create a Basic Form Structure
Let's start by creating a basic form structure using the <form>
element. We'll add the action
attribute to specify where the form data will be sent when submitted, and the method
attribute to specify the HTTP method to use.
<form action="#" method="post">
<!-- Form content will go here -->
</form>
Step 2: Add Input Fields
Now, let's add different input fields to our form. We'll use the type
attribute to specify the input type, and the name
attribute to specify the input name.
<form action="#" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<label for="gender">Gender:</label>
<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="interests">Interests:</label>
<input type="checkbox" id="reading" name="interests" value="reading">
<label for="reading">Reading</label>
<input type="checkbox" id="hiking" name="interests" value="hiking">
<label for="hiking">Hiking</label>
<label for="country">Country:</label>
<select id="country" name="country">
<option value="">Select a country</option>
<option value="usa">USA</option>
<option value="canada">Canada</option>
<option value="mexico">Mexico</option>
</select>
<button type="submit">Submit</button>
</form>
Step 3: Add Validation
Now, let's add basic validation to our form using the required
attribute and input patterns.
<form action="#" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required pattern="[a-zA-Z0-9]{5,10}">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required pattern=".{8,20}" title="Password must be between 8 and 20 characters long">
<!-- Other input fields... -->
</form>
Conclusion:
In this lab, we created a functional form with various input fields and basic validation. We explored how to use different input types, labels, and validation attributes to create a user-friendly and accessible form. By following these steps and experimenting with different input types and validation techniques, you can create robust and interactive forms for your web applications.
Practice Exercise:
Create a form that allows users to register for a newsletter. The form should include the following input fields:
- First name
- Last name
- Phone number
- Country
- Interests (checkboxes)
Add validation to ensure that the user provides a valid email address and selects at least one interest. Use different input types and attributes to enhance the user experience.
Additional Resources:
Leave a comment or ask for help if you have any questions or need further clarification on this topic.
Next topic: Introduction to semantic HTML: Importance of meaning and structure.
Images

Comments