Table of contents | |
Introduction | |
Basic Structure of a Form | |
Input Types | |
Form Validation | |
Sample Problems with Solutions |
HTML forms are used to collect user input on websites. They typically contain various input elements like text fields, checkboxes, radio buttons, dropdowns, and more. When a user submits a form, the data is sent to a server for processing.
To create a form in HTML, you need to use the '<form>' element. It acts as a container for all the input elements within the form. Here's an example of a basic form structure:
<form>
<!-- Input elements go here -->
</form>
HTML provides several input types that allow users to enter different kinds of data. Let's explore some commonly used input types and see how they are implemented.
Text inputs are used for single-line text input. Here's an example:
<label for="name">Name:</label>
<input type="text" id="name" name="name">
Password inputs are similar to text inputs, but the entered text is masked for security purposes. Here's an example:
<label for="password">Password:</label>
<input type="password" id="password" name="password">
Checkbox inputs allow users to select multiple options. Here's an example:
<label for="option1">
<input type="checkbox" id="option1" name="option[]" value="Option 1"> Option 1
</label>
<label for="option2">
<input type="checkbox" id="option2" name="option[]" value="Option 2"> Option 2
</label>
Radio buttons are used when users need to select a single option from a group. Here's an example:
<label for="option1">
<input type="radio" id="option1" name="option" value="Option 1"> Option 1
</label>
<label for="option2">
<input type="radio" id="option2" name="option" value="Option 2"> Option 2
</label>
Select dropdowns allow users to choose an option from a list. Here's an example:
<label for="country">Country:</label>
<select id="country" name="country">
<option value="USA">United States</option>
<option value="UK">United Kingdom</option>
<option value="Canada">Canada</option>
</select>
Textarea inputs are used for multi-line text input. Here's an example:
<label for="message">Message:</label>
<textarea id="message" name="message" rows="4" cols="50"></textarea>
Form validation ensures that users provide valid data before submitting a form. HTML5 provides built-in form validation attributes. Here's an example:
<input type="text" id="email" name="email" required>
Problem 1: Create a form with three text inputs: "First Name," "Last Name," and "Email Address." Add a submit button to the form.
<form>
<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName"><br>
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" name="lastName"><br>
<label for="email">Email Address:</label>
<input type="email" id="email" name="email"><br>
<input type="submit" value="Submit">
</form>
Problem 2: Create a form with a dropdown to select a favorite programming language: "JavaScript," "Python," and "Java." Add a checkbox to agree to the terms and conditions.
<form>
<label for="programmingLanguage">Favorite Programming Language:</label>
<select id="programmingLanguage" name="programmingLanguage">
<option value="JavaScript">JavaScript</option>
<option value="Python">Python</option>
<option value="Java">Java</option>
</select><br>
<label for="terms">
<input type="checkbox" id="terms" name="terms" required> I agree to the terms and conditions
</label><br>
<input type="submit" value="Submit">
</form>
20 videos|15 docs|2 tests
|
|
Explore Courses for Software Development exam
|