HTML -Eingabetypen
Dieses Kapitel beschreibt die verschiedenen Typen für das HTML- <input>
Element.
HTML-Eingabetypen
Hier sind die verschiedenen Eingabetypen, die Sie in HTML verwenden können:
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
Tipp: Der Standardwert des type
Attributs ist "text".
Eingabetyp Text
<input type="text">
definiert ein
einzeiliges Texteingabefeld :
Beispiel
<form>
<label for="fname">First name:</label><br>
<input
type="text" id="fname" name="fname"><br>
<label for="lname">Last
name:</label><br>
<input type="text" id="lname" name="lname">
</form>
So wird der obige HTML-Code in einem Browser angezeigt:
Vorname:Nachname:
Geben Sie das Kennwort ein
<input type="password">
definiert ein Passwortfeld :
Beispiel
<form>
<label for="username">Username:</label><br>
<input type="text"
id="username" name="username"><br>
<label for="pwd">Password:</label><br>
<input type="password" id="pwd" name="pwd">
</form>
So wird der obige HTML-Code in einem Browser angezeigt:
Nutzername:Passwort:
Die Zeichen in einem Passwortfeld sind maskiert (als Sternchen oder Kreise dargestellt).
Eingabetyp Senden
<input type="submit">
definiert eine Schaltfläche zum
Senden von Formulardaten an einen Formular-Handler .
Der Form-Handler ist typischerweise eine Serverseite mit einem Skript zur Verarbeitung von Eingabedaten.
Der Formular-Handler wird im action
Attribut des Formulars angegeben:
Beispiel
<form action="/action_page.php">
<label for="fname">First
name:</label><br>
<input type="text" id="fname" name="fname"
value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
So wird der obige HTML-Code in einem Browser angezeigt:
Wenn Sie das value-Attribut der Submit-Schaltfläche weglassen, erhält die Schaltfläche einen Standardtext:
Beispiel
<form action="/action_page.php">
<label for="fname">First
name:</label><br>
<input type="text" id="fname" name="fname"
value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit">
</form>
Eingangstyp zurücksetzen
<input type="reset">
definiert einen Reset-Button
, der alle Formularwerte auf ihre Standardwerte zurücksetzt:
Beispiel
<form action="/action_page.php">
<label for="fname">First
name:</label><br>
<input type="text" id="fname" name="fname"
value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
<input type="reset">
</form>
So wird der obige HTML-Code in einem Browser angezeigt:
Wenn Sie die Eingabewerte ändern und dann auf die Schaltfläche "Zurücksetzen" klicken, werden die Formulardaten auf die Standardwerte zurückgesetzt.
Eingangstyp Radio
<input type="radio">
definiert ein Optionsfeld .
Mit Optionsfeldern kann ein Benutzer NUR EINE aus einer begrenzten Anzahl von Auswahlmöglichkeiten auswählen:
Beispiel
<p>Choose your favorite Web language:</p>
<form>
<input type="radio" id="html" name="fav_language"
value="HTML">
<label for="html">HTML</label><br>
<input
type="radio" id="css" name="fav_language" value="CSS">
<label
for="css">CSS</label><br>
<input type="radio" id="javascript"
name="fav_language" value="JavaScript">
<label for="javascript">JavaScript</label>
</form>
So wird der obige HTML-Code in einem Browser angezeigt:
Kontrollkästchen Eingabetyp
<input type="checkbox">
definiert ein Kontrollkästchen .
Kontrollkästchen ermöglichen es einem Benutzer, NULL oder MEHR Optionen aus einer begrenzten Anzahl von Auswahlmöglichkeiten auszuwählen.
Beispiel
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input
type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2">
I have a car</label><br>
<input type="checkbox"
id="vehicle3" name="vehicle3"
value="Boat">
<label for="vehicle3"> I have a boat</label>
</form>
So wird der obige HTML-Code in einem Browser angezeigt:
Eingabetyp-Schaltfläche
<input type="button">
definiert eine Schaltfläche :
Beispiel
<input type="button" onclick="alert('Hello World!')" value="Click Me!">
So wird der obige HTML-Code in einem Browser angezeigt:
Eingabetyp Farbe
Der <input type="color">
wird für Eingabefelder verwendet, die eine Farbe enthalten sollen.
Je nach Browserunterstützung kann im Eingabefeld ein Farbwähler erscheinen.
Beispiel
<form>
<label for="favcolor">Select your favorite
color:</label>
<input type="color" id="favcolor" name="favcolor">
</form>
Eingabetyp Datum
Der <input type="date">
wird für Eingabefelder verwendet, die ein Datum enthalten sollen.
Je nach Browserunterstützung kann im Eingabefeld eine Datumsauswahl erscheinen.
Beispiel
<form>
<label for="birthday">Birthday:</label>
<input
type="date" id="birthday" name="birthday">
</form>
Sie können auch die Attribute min
und verwenden max
, um Datumsbeschränkungen hinzuzufügen:
Beispiel
<form>
<label for="datemax">Enter a date before
1980-01-01:</label>
<input type="date" id="datemax" name="datemax"
max="1979-12-31"><br><br>
<label for="datemin">Enter a date after
2000-01-01:</label>
<input type="date" id="datemin" name="datemin"
min="2000-01-02">
</form>
Eingabetyp Datetime-local
Das <input type="datetime-local">
gibt ein Datums- und Zeiteingabefeld ohne Zeitzone an.
Je nach Browserunterstützung kann im Eingabefeld eine Datumsauswahl erscheinen.
Beispiel
<form>
<label for="birthdaytime">Birthday (date and
time):</label>
<input type="datetime-local" id="birthdaytime" name="birthdaytime">
</form>
Geben Sie E-Mail ein
Der <input type="email">
wird für Eingabefelder verwendet, die eine E-Mail-Adresse enthalten sollen.
Je nach Browserunterstützung kann die E-Mail-Adresse beim Absenden automatisch validiert werden.
Einige Smartphones erkennen den E-Mail-Typ und fügen ".com" zur Tastatur hinzu, um der E-Mail-Eingabe zu entsprechen.
Beispiel
<form>
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email">
</form>
Eingabetyp Datei
Der <input type="file">
definiert ein Dateiauswahlfeld und eine Schaltfläche "Durchsuchen" für Datei-Uploads.
Beispiel
<form>
<label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile">
</form>
Eingabetyp ausgeblendet
Der <input type="hidden">
definiert ein verstecktes Eingabefeld (für einen Benutzer nicht sichtbar).
Ein ausgeblendetes Feld ermöglicht es Webentwicklern, Daten einzufügen, die von Benutzern nicht gesehen oder geändert werden können, wenn ein Formular gesendet wird.
Ein ausgeblendetes Feld speichert häufig, welcher Datenbankeintrag aktualisiert werden muss, wenn das Formular gesendet wird.
Hinweis: Obwohl der Wert dem Benutzer nicht im Inhalt der Seite angezeigt wird, ist er mit den Entwicklertools oder der „Quelle anzeigen“-Funktion eines beliebigen Browsers sichtbar (und kann bearbeitet werden). Verwenden Sie keine versteckten Eingaben als Sicherheitsmaßnahme!
Beispiel
<form>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<input
type="hidden" id="custId" name="custId" value="3487">
<input
type="submit" value="Submit">
</form>
Eingabetyp Monat
Das <input type="month">
erlaubt dem Benutzer, einen Monat und ein Jahr auszuwählen.
Je nach Browserunterstützung kann im Eingabefeld eine Datumsauswahl erscheinen.
Beispiel
<form>
<label for="bdaymonth">Birthday (month and
year):</label>
<input type="month" id="bdaymonth" name="bdaymonth">
</form>
Typnummer eingeben
Der <input type="number">
definiert ein
numerisches Eingabefeld.
You can also set restrictions on what numbers are accepted.
The following example displays a numeric input field, where you can enter a value from 1 to 5:
Example
<form>
<label for="quantity">Quantity (between 1 and
5):</label>
<input type="number" id="quantity" name="quantity"
min="1" max="5">
</form>
Input Restrictions
Here is a list of some common input restrictions:
Attribute | Description |
---|---|
checked | Specifies that an input field should be pre-selected when the page loads (for type="checkbox" or type="radio") |
disabled | Specifies that an input field should be disabled |
max | Specifies the maximum value for an input field |
maxlength | Specifies the maximum number of character for an input field |
min | Specifies the minimum value for an input field |
pattern | Specifies a regular expression to check the input value against |
readonly | Specifies that an input field is read only (cannot be changed) |
required | Specifies that an input field is required (must be filled out) |
size | Specifies the width (in characters) of an input field |
step | Specifies the legal number intervals for an input field |
value | Specifies the default value for an input field |
You will learn more about input restrictions in the next chapter.
The following example displays a numeric input field, where you can enter a value from 0 to 100, in steps of 10. The default value is 30:
Example
<form>
<label for="quantity">Quantity:</label>
<input
type="number" id="quantity" name="quantity" min="0" max="100" step="10"
value="30">
</form>
Input Type Range
The <input type="range">
defines a control for entering a number whose exact value is not important (like a slider control). Default
range is 0 to 100. However, you can set restrictions on what
numbers are accepted with the min
, max
, and step
attributes:
Example
<form>
<label for="vol">Volume (between 0 and 50):</label>
<input type="range" id="vol" name="vol" min="0" max="50">
</form>
Input Type Search
The <input type="search">
is used for search fields (a search field behaves like a regular text field).
Example
<form>
<label for="gsearch">Search Google:</label>
<input type="search" id="gsearch" name="gsearch">
</form>
Input Type Tel
The <input type="tel">
is used for input fields that should contain a telephone number.
Example
<form>
<label for="phone">Enter your phone number:</label>
<input type="tel" id="phone" name="phone"
pattern="[0-9]{3}-[0-9]{2}-[0-9]{3}">
</form>
Input Type Time
The <input type="time">
allows the user to select a time (no time zone).
Depending on browser support, a time picker can show up in the input field.
Example
<form>
<label for="appt">Select a time:</label>
<input type="time" id="appt" name="appt">
</form>
Input Type Url
The <input type="url">
is used for input fields that should contain a URL address.
Depending on browser support, the url field can be automatically validated when submitted.
Some smartphones recognize the url type, and adds ".com" to the keyboard to match url input.
Example
<form>
<label for="homepage">Add your homepage:</label>
<input type="url" id="homepage" name="homepage">
</form>
Input Type Week
The <input type="week">
allows the user to select a week and year.
Depending on browser support, a date picker can show up in the input field.
Example
<form>
<label for="week">Select a week:</label>
<input type="week" id="week" name="week">
</form>
HTML Exercises
HTML Input Type Attribute
Tag | Description |
---|---|
<input type=""> | Specifies the input type to display |