11 Forms
11 Forms
Forms -->
Forms in HTML
1. <form>:
- It is a semantic element that defines the beginning of the form.
- It contains all the form-related elements.
Example:
2. <label>:
- Specifies the purpose of the input field.
- It's linked to the <input> field using the for attribute, which matches the id
of the input element.
Example:
<label for="userName">Name:</label>
<input type="text" id="userName">
3. <input>:
- Used to collect user data.
- There are various types such as text, email, password, number, etc.
Example:
Example:
<label for="skills">Skills:</label>
<select id="skills">
<option value="html">HTML</option>
<option value="css">CSS</option>
</select>
5. <option>:
- Defines the options within a dropdown list.
6. <fieldset>:
- Groups form elements together and provides a border around them.
Example:
<fieldset>
<legend>Personal Information</legend>
<label for="name">Name:</label>
<input type="text" id="name">
</fieldset>
7. <legend>:
- Provides a caption for the <fieldset> group.
8. <textarea>:
- Used to collect multi-line text input.
- The rows and cols attributes specify the visible height and width.
Example:
Example:
<button type="submit">Submit</button>
Example:
<form action="/submit-data">
2. method: Specifies how the data is sent to the server (GET or POST).
- GET: Sends data in the URL, not secure.
- POST: Sends data securely in the HTTP body.
Example:
<form method="post">
Example:
<form autocomplete="on">
Attributes of the <input> Tag:
1. type: Specifies the type of input field (e.g., text, email, password, etc.).
2. id: Provides a unique identifier for the input element.
3. name: Associates a name with the input field, used for form submission.
4. value: Defines the initial value of the input field.
5. required: Makes the field mandatory to fill before submission.
6. readonly: Prevents users from modifying the input field value.
7. disabled: Disables the input field.
8. min, max, minlength, maxlength: Used to define the minimum and
maximum values or lengths for input.
9. placeholder: Displays a hint text inside the input field.
The type attribute of the <input> tag in HTML determines the kind of input
field displayed on the webpage. Here are the common values of the type
attribute:
1. text:
- Allows users to enter regular text.
- Default input type.
Example:
<input type="text">
2. email:
- Used for email input. It automatically validates the input for an email
format.
Example:
<input type="email">
3. password:
- Displays input as masked characters (dots or asterisks), hiding the user's
input.
Example:
<input type="password">
4. number:
- Allows users to enter numerical values. It can have min, max, and step
attributes for validation.
Example:
5. tel:
- Allows users to input telephone numbers. It doesn't enforce specific
validation but provides a numeric keyboard on mobile devices.
Example:
<input type="tel">
6. radio:
- Creates a radio button, allowing users to select one option from a group.
Example:
Example:
8. button:
- Creates a clickable button. Typically, you can use JavaScript to define what
happens when it’s clicked.
Example:
9. submit:
- Creates a submit button that submits the form data to the server.
Example:
10. reset:
- Resets all the form fields to their default values.
Example:
Example:
<input type="date">
12. time:
- Allows users to select a time.
Example:
<input type="time">
13. week:
- Allows users to select a specific week in a year.
Example:
<input type="week">
14. month:
- Allows users to select a specific month and year.
Example:
<input type="month">
15. color:
- Allows users to select a color from a color picker.
Example:
<input type="color">
16. image:
- Creates a submit button in the form of an image.
Example:
17. file:
- Allows users to upload files from their device.
Example:
<input type="file">
18. range:
- Creates a slider input that allows users to select a numeric value within a
specific range.
Example:
<fieldset>
<legend>student info</legend>
<br><br>
<label for="dob">Date Of Birth</label>
<input type="date" name="dob" id="dob">
<br><br>
<label for="">Gender</label>
<input type="radio" name="gender" id="male" value="male">
<label for="male">Male</label>
<input type="radio" name="gender" id="female" value="female">
<label for="female">Female</label>
<input type="radio" name="gender" id="others" value="others">
<label for="others">Others</label>
<br><br>
</select>
<br><br>
<br><br>
<label for="">Salary</label>
<input type="range" name="salary" id="" min="250000" max="400000000">
<br><br>
<button type="submit">SignUp</button>
<button type="reset">reset</button>
</fieldset>
</form>
</body>
</html>