Learn HTML Forms In 25 Minutes

Web Dev Simplified
8 Jun 201924:55

Summary

TLDRThis video tutorial offers a comprehensive guide to creating HTML forms, starting with the essential login form. It covers various input types, including text, email, number, date, and password, highlighting browser-based validation and user experience features. The tutorial also explores checkboxes, radio buttons, select elements, textareas, and hidden inputs, explaining their uses and configurations. Additionally, it touches on file uploads and少见 input types like telephone and URL, concluding with practical tips for enhancing form functionality and user interaction.

Takeaways

  • 📝 Forms are crucial for user interaction on websites, allowing data submission.
  • 🔑 The 'form' tag is used to define where inputs will be submitted, with the 'action' attribute specifying the destination.
  • 🔍 Input elements within a form do not require closing tags and are self-closing.
  • 🏷️ The 'label' element improves accessibility and usability by linking text to form controls.
  • 🔄 Div elements can be used to organize form inputs into a multi-line layout.
  • 🔒 The 'type' attribute in input tags defines the type of data expected, such as 'text', 'password', 'email', etc.
  • 🔄 The 'submit' button in a form triggers data submission, and 'reset' can clear form inputs to default values.
  • 🔒 The 'required' attribute enforces mandatory field completion before form submission.
  • 🔑 The 'name' attribute in input tags identifies the data for processing on submission.
  • 🔗 The 'for' attribute in 'label' tags associates the label with a specific form control.
  • 📅 Date and time input types provide built-in validation and user interfaces for date selection.
  • 🔢 Number input types restrict input to numeric values and can include attributes like 'min', 'max', and 'step' for range and increment control.
  • 🗸 Checkboxes and radio buttons are used for multiple-choice questions, with the same 'name' attribute ensuring single selection in radio buttons.
  • 👁️ The 'select' element allows users to choose one or multiple options from a dropdown list.
  • 📝 The 'textarea' element is for inputting large amounts of text, including new lines.
  • 🔍 The 'hidden' input type is used for data that should not be visible to the user but is submitted with the form.
  • 📁 The 'file' input type enables file uploads, requiring the form to be set to 'multipart/form-data' for proper submission.
  • 📞 Specialized input types like 'tel' for telephone numbers and 'url' for URLs optimize mobile keypads for easier data entry.
  • 🎨 The 'color' input type provides a color picker for users to select a color.

Q & A

  • What is the purpose of forms in a website?

    -Forms are one of the most important parts of a website as they allow users to input and submit information to the site, facilitating user interaction and data collection.

  • How do you create a basic HTML page for a form?

    -You can create a basic HTML page for a form by using Emmet or by manually writing the boilerplate code, starting with a 'form' tag and including input elements within it.

  • What is the default action of a form if left unspecified?

    -If the action attribute is left unspecified in a form tag, the form will submit to the current page that the user is on.

  • How do you create a label for an input element?

    -You create a label for an input element by using the 'label' tag and associating it with the input field using the 'for' attribute, which matches the input's 'id'.

  • What is the purpose of the 'submit' button in a form?

    -The 'submit' button in a form is used to send the form data to a server for processing. It triggers the submission of the form.

  • Why is it necessary to specify the 'name' attribute for input elements?

    -The 'name' attribute for input elements is necessary to identify the data submitted by the form, making it accessible for processing on the server side.

  • What is the difference between 'get' and 'post' methods in forms?

    -The 'get' method appends form data to the URL and is used for simple data retrieval, while the 'post' method sends data in the body of the request and is used for secure transmission of sensitive information.

  • How can you validate email addresses using form input elements?

    -You can validate email addresses by specifying the type of an input element as 'email', which enables built-in browser validation to ensure the input is in a valid email format.

  • What is the purpose of the 'required' attribute in form input elements?

    -The 'required' attribute is used to enforce that the user must fill out a specific field before the form can be submitted, preventing the submission of incomplete data.

  • How can you make an input element default to a certain value?

    -You can set a default value for an input element by using the 'value' attribute, which pre-fills the input field with the specified value when the page loads.

  • What is the function of the 'placeholder' attribute in input elements?

    -The 'placeholder' attribute provides a hint or example text within an input field that describes the expected value, which disappears when the user starts typing.

  • How do you create a checkbox input element?

    -A checkbox input element is created using the 'input' tag with the type set to 'checkbox'. It is typically associated with a 'label' for better accessibility and user experience.

  • What is the difference between checkboxes and radio buttons?

    -Checkboxes allow users to select multiple options, while radio buttons are used when only one option can be selected at a time from a group of choices.

  • How do you create a select element with multiple options?

    -A select element is created with the 'select' tag, and options are added using the 'option' tag. To allow multiple selections, the 'multiple' attribute can be added to the 'select' element.

  • What is a textarea used for in a form?

    -A textarea is used for inputting larger amounts of text, including multiple lines, and is ideal for collecting information that requires more space than a single-line input.

  • What is the purpose of the 'hidden' input type?

    -The 'hidden' input type is used to include data in the form submission that should not be visible or interactable by the user, typically used for server-side processing or JavaScript manipulation.

  • How can you enable file uploads in a form?

    -File uploads can be enabled in a form by using an 'input' element with the type set to 'file' and ensuring the form has the 'enctype' attribute set to 'multipart/form-data'.

  • What are some of the less common input types mentioned in the script?

    -Some of the less common input types mentioned include 'tel' for telephone numbers, 'url' for URLs, and 'color' for color selection, each providing specific keyboard layouts or validation.

Outlines

00:00

🌐 Introduction to HTML Forms

The paragraph introduces the importance of information and forms in web development. It outlines the process of creating a basic login form, including setting up an HTML page, using the form tag, and adding input elements for user data submission. The video demonstrates how to use input elements for user names and passwords, and how to include a submit button. It also covers the use of div tags for layout and the concept of form submission through the 'action' attribute, highlighting the difference between GET and POST methods.

05:01

🔑 Enhancing Form Usability

This section delves into improving form usability with labels and IDs, explaining how to associate labels with input fields for accessibility and screen reader support. It also discusses the use of input types, such as 'password' to hide input characters, and 'submit' for form submission. The paragraph further explores the use of the 'value' attribute for default values and 'placeholder' for providing hints to users. It concludes with the importance of the 'required' attribute to ensure form fields are not left empty.

10:02

📊 Form Input Types and Validation

The paragraph explores various HTML input types, including 'email' for email address validation, 'number' for numeric input with min/max constraints, and 'date' for date selection with built-in date pickers. It also touches on the 'checkbox' and 'radio' input types, explaining their use for multiple choices and single selections, respectively. The 'select' element is introduced for dropdown menus, and 'textarea' for multi-line text inputs. The paragraph concludes with a brief mention of the 'hidden' input type.

15:02

🖼️ Advanced Form Elements

This section focuses on advanced form elements such as 'file' input for uploading files, 'tel' for telephone number input with a specialized keypad on mobile, 'url' for website URLs, and 'color' for color selection. It emphasizes the 'enctype' attribute necessary for file uploads to ensure the form data is sent in multiple parts. The paragraph also mentions the 'text' input type, which is not explicitly detailed but is implied to be for general text entry.

20:03

🎉 Wrapping Up HTML Forms

The final paragraph summarizes the video's coverage of HTML forms, highlighting the variety of input types and form elements discussed. It encourages viewers to explore more resources and subscribe for further web development tutorials, thanking them for their time and interest in the topic.

Mindmap

Keywords

💡Forms

Forms in the context of web development are used to collect user inputs and are a fundamental aspect of user interaction with websites. In the video, forms are the central theme, with a focus on creating and understanding various form elements and their attributes.

💡Input Elements

Input elements are HTML tags used within forms to capture user data. The video script discusses different types of input elements such as text, password, email, and number, each serving a specific purpose in data collection and providing examples of how they are implemented in a form.

💡Label Element

The label element is used to define a label for an input element and is crucial for accessibility. In the script, it is mentioned that clicking on a label should highlight the associated input field, which is important for screen readers and user experience.

💡Action Attribute

The action attribute in a form tag specifies where to send the form-data when a form is submitted. The video explains how to use the action attribute to direct the form submission to a specific page, such as 'results.html'.

💡Method Attribute

The method attribute defines the HTTP method used when sending form-data. The script covers 'GET' and 'POST' methods, explaining that 'GET' appends form data to the URL, while 'POST' sends data in the request body, and the implications of each for form submission.

💡Placeholder

The placeholder attribute provides a hint to the user about what should be entered in the input field. The video script uses the placeholder to give an example of how to guide users on filling out form fields, such as 'user name' in the username input field.

💡Required Attribute

The required attribute is a boolean attribute that makes an input field mandatory to fill out before submitting the form. The script demonstrates its use to ensure that users cannot submit the form without providing necessary information, such as a username or password.

💡Type Attribute

The type attribute of the input element defines the type of data expected, such as 'text', 'password', 'email', or 'number'. The video script explains how different types of input fields are used for different kinds of data, affecting both user interface and data validation.

💡Button Element

The button element in HTML is used to create clickable buttons. The script distinguishes between 'submit' and 'reset' types of buttons, explaining their functions in the context of a form — submitting form data or resetting form fields to default values.

💡Validation

Validation in the context of forms refers to the process of checking that the entered data conforms to specified rules. The video script discusses built-in browser validation for email and number inputs, as well as custom validation using the required attribute.

💡File Input

A file input allows users to upload files through a form. The script explains how to implement a file input and mentions the need for setting the 'enctype' attribute of the form to 'multipart/form-data' to handle file uploads properly.

💡Hidden Input

A hidden input is an input element that is not displayed to the user but is sent along with the form data. The script describes the use of hidden inputs for sending data that the user does not need to see or interact with.

💡TextArea

The textarea element allows for multi-line input from the user, unlike single-line input fields. The video script demonstrates how to create a textarea for users to enter longer text, such as a biography or feedback.

💡Select Element

The select element creates a drop-down list of options. The script explains how to use the select element to provide users with a list of choices, such as different eye colors, and how to allow multiple selections using the 'multiple' attribute.

💡Radio Button

Radio buttons are used when only one option can be selected from a group of choices. The video script illustrates how radio buttons with the same name attribute ensure that only one option can be chosen, such as selecting a gender.

💡Checkbox

Checkboxes allow users to select multiple options from a group. The script shows how to implement checkboxes for selecting favorite foods and contrasts them with radio buttons, which require the same name attribute for a single selection.

Highlights

Introduction to the importance of forms in web development.

Demonstration of creating a basic login form using HTML.

Explanation of how to use the form tag to group inputs for submission.

Use of input elements for user input without the need for closing tags.

Inclusion of label elements to improve accessibility and user experience.

Creation of a submit button to allow form submission.

Utilization of div tags for layout purposes to organize form elements.

Discussion on setting the action attribute to define form submission destination.

Clarification on the use of GET vs POST methods for form submission.

Introduction of JavaScript for rendering form results on a results page.

The necessity of specifying input names for form data retrieval.

Use of the 'for' attribute to associate labels with form inputs.

Different types of input elements like email, number, and date for specific data validation.

The functionality of checkboxes and radio buttons for multiple or single selections.

Explanation of the select element for choosing one option from multiple.

Introduction of the textarea element for larger text inputs.

Use of the hidden input element for including data not visible to the user.

File input element for uploading files and setting the enctype attribute.

Additional input types like tel for phone numbers and color for color selection.

Conclusion summarizing the comprehensive coverage of HTML forms in the video.

Transcripts

play00:00

information is key which is why forms

play00:02

are one of the most important parts of a

play00:03

website in today's video I'm going to

play00:05

show you everything you need to know

play00:06

about how to create forms and elements

play00:08

inside of those forms and if you're new

play00:10

around here make sure to subscribe for

play00:11

more videos where I simplified the web

play00:13

for you let's get started now

play00:17

to get started with forms let's build

play00:20

one of the most common types of forms

play00:21

which is a login form to do that we

play00:23

first need to create a page to load it

play00:25

on so we're just create a page called

play00:27

index.html and in here if you're envious

play00:29

code or you're using Emmet if you just

play00:31

type exclamation point and hit enter

play00:33

it'll generate all the boilerplate code

play00:34

you need for a basic HTML page

play00:36

we could just change the title here to

play00:38

form because this is going to be

play00:39

demonstrated in forms now to use a form

play00:41

the first thing you need to do is open

play00:43

up a form tag so you can just type in

play00:45

here form close it off and this is going

play00:47

to be a form tag in all the inputs that

play00:49

you put inside of this form are going to

play00:50

be submitted to wherever your form goes

play00:52

to by default if you just leave your

play00:54

form tag blank like this it's going to

play00:56

submit to the page that you're currently

play00:57

on so in a study here let's put our

play01:00

different inputs so we can use the input

play01:02

element which is going to allow us to

play01:03

input into our page from our user and

play01:06

what we want to do is we want an input

play01:07

element and this one's going to be the

play01:08

name and the thing with input elements

play01:10

is they don't actually need to be closed

play01:11

because they don't actually allow you to

play01:13

specify anything inside of the tags

play01:15

there are self closing tags so if we

play01:17

want to add a label to our input we need

play01:19

to do is we need to use what's called a

play01:20

label element so let's have our first

play01:22

one beat our user name so what you're

play01:24

going to put a name here and we can just

play01:26

copy this down because we're going to

play01:27

need a second one which is going to be

play01:28

for our password so we can type and

play01:30

password here and now we're almost

play01:32

complete with the basics of our form but

play01:34

we need some way to submit our form so

play01:36

we're going to create a button to do

play01:37

that so let's just create a button here

play01:39

and we're just going to call this submit

play01:41

for the text of our button now to

play01:43

actually test this page we can just

play01:44

right click and open with live server

play01:46

which is an extension from Visual Studio

play01:47

code that you can download once we have

play01:50

that open we can see over here that if

play01:52

we expand this a little bit we have our

play01:53

name field we have our password field

play01:55

and we have our submit button and right

play01:57

now they're all clobbered onto the same

play01:58

line so let's use some divs which is

play02:00

just another basic tag which will allow

play02:02

us to separate these onto multiple lines

play02:03

so if we put some code inside of this

play02:06

div it's going to end up on the next

play02:07

line and we can do the exact same thing

play02:09

down here with our password let's create

play02:11

another div to wrap our password element

play02:13

in oops copy this paste it in there and

play02:18

when we save you see it populates over

play02:20

here on new lines now we can enter name

play02:23

information password information and

play02:24

click Submit and you'll see it'll submit

play02:26

it to the page that we're already on but

play02:28

while technically this does work it's

play02:30

not at all

play02:31

you want to build forms in actuality

play02:32

almost always when you build the form

play02:34

you're going to want your form to submit

play02:36

somewhere other than the page that

play02:38

you're currently on so in order to do

play02:39

that we have a keyword here in attribute

play02:41

which is called action and the action is

play02:43

going to be where your form is

play02:44

submitting to and in our case we're just

play02:46

going to create a page here which is

play02:47

called results HTML and this is going to

play02:50

be where we want our form to submit to

play02:52

the next thing that you almost always

play02:53

are going to specify on a form is the

play02:55

method you want your form to use and

play02:57

this is either going to be get or a post

play02:59

with a get request it's going to append

play03:01

things to the URL and it's going to send

play03:03

it to another page in your site while

play03:05

the post is going to be useful for when

play03:07

you have a server where you need to save

play03:08

some information if for example we were

play03:10

to use post here so if we change this to

play03:12

post and we try to submit something you

play03:15

see that we're immediately going to get

play03:16

an error because browsers can't render

play03:17

post requests they can only render get

play03:19

requests and we don't have a server so

play03:21

what we need to do is we need to use a

play03:23

get request here instead of a post

play03:24

request and now this is going to work as

play03:26

soon as we create our actual page so

play03:29

let's create that results HTML page and

play03:31

I'm actually just going to copy over

play03:32

here

play03:33

it results HTML page this is a very

play03:35

basic page all it has is a link that

play03:36

goes back to our form and it has a

play03:38

little bit of JavaScript which is going

play03:40

to render the results from our HTML form

play03:42

from before so now let's go back over

play03:45

here refresh our page and we want to

play03:47

just make sure we click Submit and

play03:49

you'll see that nothing is being shown

play03:50

up for our form fields but we do have

play03:52

that button to go back and the reason

play03:54

nothing is showing up for our form

play03:55

fields is because in order for an input

play03:57

to actually show up as a form what we

play03:59

need to do is we need to specify the

play04:00

name for that input so let's do that now

play04:02

the name here is just going to be a name

play04:04

for this first one because we just want

play04:06

it to be whatever we're submitting to

play04:07

our form so we're gonna use this as name

play04:09

and down here for our password we're

play04:11

gonna set the name of this one equal to

play04:12

password now if we say bet and come over

play04:15

here and we type in for example a

play04:17

username and we type in a password let's

play04:20

just say that's that and we click Submit

play04:21

you'll see name is set to username and

play04:23

password is set to password if you can

play04:26

see in the URL it's setting those

play04:27

parameters here name equals user name

play04:29

and password equals password and that's

play04:31

what a get request does is it sets it in

play04:33

the URL here while if we did the post

play04:35

request here this is actually going to

play04:36

set it in the body so if we refresh this

play04:39

enter in some information and click

play04:41

Submit again we're going to get that

play04:42

error because we don't have a server

play04:44

accepting any requests so you need a

play04:46

server if you're going to use post so

play04:47

for the rest of this example we're going

play04:49

to be using git for all of our different

play04:51

requests so now with that out of the way

play04:52

we have our form set up properly we have

play04:54

our inputs with actual names so that we

play04:56

can use these and render them later when

play04:58

we submit our form but our labels aren't

play05:00

actually doing anything very useful

play05:02

because normally if you click on a label

play05:03

it should actually highlight the field

play05:05

that's being labeled for this is really

play05:07

helpful for not only users but also for

play05:09

screen readers in order to do this we

play05:11

just need a set of four attribute here

play05:13

and we said it's the idea the element

play05:14

that we want to highlight whenever we

play05:16

click on the label so in our case we're

play05:18

gonna say name and then we need to give

play05:19

our element here that ID which is just

play05:22

going to be named now if we click on our

play05:24

label it's actually going to highlight

play05:25

the input element that that label is for

play05:27

another way that you can do this is if

play05:29

you don't want to have that four

play05:30

attribute you can actually nest your

play05:32

element inside the label so if we come

play05:34

in here and we wrap our label around our

play05:36

input element so we have both our

play05:38

password string and our input element

play05:40

inside of the label now when we click

play05:42

the label you see it's going to

play05:43

highlight that password field for us

play05:45

almost always you're going to see people

play05:47

use the for attribute instead of

play05:48

wrapping inside of it just because it's

play05:50

cleaner and easier to style but just so

play05:53

you know that you can do this and a lot

play05:54

of people do this when it comes to

play05:55

checkboxes and radio buttons which we're

play05:57

going to get to later in this tutorial

play05:58

and speaking of different types of input

play06:00

elements one thing you only knows is

play06:02

that our input here does not have any

play06:04

type specified for it which is why it's

play06:05

defaulting to a text input here but you

play06:08

always want to be explicit in what type

play06:10

of input you're using so you always want

play06:12

to specify a type field and the input

play06:14

tag is very versatile because you can

play06:16

have many many different types of inputs

play06:18

but in our example we just want to have

play06:19

a text input here for the name but for

play06:22

the password normally when you're on a

play06:23

website when you type in your password

play06:25

it's actually hidden and that's because

play06:27

there's a type which is called password

play06:29

which actually by default hides the

play06:31

characters for you so now if we save

play06:32

this and we enter our username you see

play06:34

it works just fine when we enter a

play06:35

password

play06:36

you see it's hiding our characters for

play06:38

us and that's because we're using type

play06:39

of password for our input element and

play06:41

lastly the only thing left to make this

play06:44

login form correct is that we need to

play06:46

change our button down here because we

play06:48

want this to be a submit button so we

play06:50

can just come in here we can type in the

play06:52

type of submit and what this says is

play06:54

this as it's going to submit our form

play06:55

and when we hit enter for example

play06:57

anywhere

play06:58

our form so if I type in something here

play06:59

and I click the enter key it's also

play07:01

going to submit our form which is why

play07:03

having type submit' on your button is

play07:04

really useful and it also allows you to

play07:07

have other buttons in your form that

play07:08

don't necessarily submit your form

play07:10

because this one right here is

play07:12

submitting it since it has the type of

play07:13

submit another type of button that's

play07:15

really useful and really under no one is

play07:17

going to be a button with a type of

play07:19

reset and if we just set that here we're

play07:22

just going to set the text here to reset

play07:23

and we go back to our form and we just

play07:26

type in some text

play07:27

if we click reset it sets everything

play07:28

back to its defaults and as you can see

play07:30

our defaults for us are just going to be

play07:31

completely blank but what happens if for

play07:34

example you want to default the field

play07:36

for the user let's say you already know

play07:38

what the user name is for that user and

play07:40

you want to set that by default that's

play07:42

where the value property is going to

play07:44

come in so you just type in a value and

play07:45

anything you set in here is going to be

play07:47

the default value for that property so

play07:49

let's set the default here to just be

play07:51

user now when we refresh our page you

play07:53

see the user is being the defaulted name

play07:56

and the best part is is if we change

play07:58

this and click reset it goes back to the

play08:00

default value we have set in our code

play08:02

for the HTML another thing that's really

play08:04

useful is to have a placeholder value

play08:06

for example if we have this user's name

play08:08

here let's say that we don't actually

play08:10

know what their name is but we want to

play08:11

put in a placeholder this is just

play08:12

something that's going to show up to

play08:14

kind of let the user know a little bit

play08:15

more information about what they want to

play08:17

put in here so we're gonna say our place

play08:19

while there is just user name and as you

play08:20

can see it fills that in in a lightish

play08:22

grade text and as soon as the user typed

play08:24

something else it completely removes the

play08:25

placeholder and if they remove

play08:27

everything it goes back to having that

play08:28

placeholder just to reiterate a little

play08:30

bit on the points that we touched on a

play08:31

placeholder is really useful for when

play08:33

you want to give a little bit more

play08:34

information to your user while value

play08:36

that you set so for example we change

play08:39

this here to value this is really useful

play08:42

if you already know what the value is or

play08:44

you want to default this as something

play08:45

that is going to be submitted with your

play08:47

form because if we click Submit it

play08:48

submits that user name value but if we

play08:51

change this to be placeholder for

play08:52

example instead and we click submit it

play08:54

does not submit the placeholder only the

play08:56

value another thing you'll notice is

play08:58

that we're able to submit a blank name

play09:00

and a blank password which in a login

play09:01

form you would want to throw some kind

play09:03

of error to the user telling them they

play09:04

can't submit blank information and

play09:06

luckily this is really easy to do

play09:08

there's an attribute which is called

play09:10

required and this is required attribute

play09:12

is

play09:12

or seeing the user to actually enter

play09:13

this information let's remove this

play09:15

placeholder so it's a little easier to

play09:16

see and if we save it and click Submit

play09:18

you'll see that we get an error that

play09:19

says please fill out this field and if

play09:21

we fill it out and click Submit now

play09:23

it'll let it go through so we want to

play09:24

add that required attribute to both our

play09:26

password and to our name field here so

play09:28

now they even if they enter just a name

play09:30

no password it's still going to give

play09:32

them that error until they enter that

play09:33

password as well now as I mentioned

play09:35

earlier in this video the input element

play09:37

here has many many different types that

play09:39

it can be so let's go through and

play09:41

analyze some of the different types that

play09:42

we have by starting with one of the most

play09:44

common types which is going to be the

play09:45

email type let's create another div here

play09:47

this Dib is going to be used to ask for

play09:49

the email so we're gonna have a label

play09:50

we're gonna use for email and in here

play09:53

we're just going to put email as the

play09:54

label and then we're gonna create

play09:56

another input and this is going to have

play09:58

a type here of email and this is going

play10:00

to verify email addresses for us by

play10:02

default let's make sure we set the name

play10:03

to email so we can use it when we submit

play10:05

it and we also need the ID here to match

play10:08

what we set for our for so we're going

play10:10

to use email in there and let's just

play10:11

make it required because we always want

play10:13

our user to send an email now if we say

play10:15

that you see our email field is showing

play10:16

up here and if we type in something for

play10:18

example I was typing everything and we

play10:20

click Submit you see that it says to

play10:22

please actually make sure that this is a

play10:23

valid email address so by default our

play10:25

browser is doing email validation for us

play10:27

now it's not the greatest email

play10:29

validation in the entire world but it's

play10:31

a whole lot better than having nothing

play10:32

and also when a users on a phone it's

play10:35

going to give them a keyboard which is

play10:37

actually specified for entering emails

play10:38

as opposed to just the generic text

play10:40

keyboard other than that though email is

play10:42

very similar to the text field that

play10:44

we've already covered now let's move on

play10:45

to a little bit more complicated of a

play10:47

field which is the number field so let's

play10:49

again enter another div in here with a

play10:51

label this is going to be for what we're

play10:53

going to put as our age because we want

play10:54

to ask the user for their age and we're

play10:56

going to create an input again for doing

play10:57

that and this is going to have a type

play10:59

here of a number and let's make sure we

play11:02

set a name which is going to say age and

play11:04

an ID which is going to be age and let's

play11:07

just not make this from a required

play11:08

because we don't think that they need to

play11:09

enter this and if we save you see we get

play11:11

aged and immediately you'll notice we

play11:13

have up and down arrows to be able to

play11:14

specify where our age is going as well

play11:16

as where you can use the arrow keys to

play11:18

modify this and that's because we're

play11:19

using the type of number when we specify

play11:21

this input also if I try to enter

play11:23

different characters that are not

play11:24

numbers it won't allow me to

play11:26

to them another really useful thing that

play11:27

we can do when specifying number

play11:29

elements as we can specify minimum and

play11:31

maximum so obviously we don't want our

play11:33

age to be below zero so we're going to

play11:35

set the minimum here to be one and we

play11:37

also don't want it to be above a certain

play11:39

number so we're gonna set the maximum to

play11:40

just say 200 no one's over 200 years old

play11:43

so now if we save that and come over

play11:45

here and we try to decrease this you see

play11:47

that one is the lowest we can go if for

play11:49

example I enter negative 10 make sure

play11:51

all the required fields are entered and

play11:52

hit submit

play11:52

let's make sure this is actually an

play11:54

email and now we click Submit you'll see

play11:57

that says the value must be greater than

play11:58

10 but let's say we put 201 in here

play12:01

click Submit you'll see that it must be

play12:03

less than 200 so these validations are

play12:05

built into the browser another

play12:06

interesting thing we can do is what's

play12:08

called step and by default the step is

play12:10

going to be equal to 1 so that means

play12:12

when we click our up and down arrow keys

play12:13

it's going to increase and decrease by 1

play12:15

well for example we could change us what

play12:17

if we wanted to be by 5 so if we save

play12:19

this now when I click up it's going to

play12:21

increase by 5 at a time as you can see

play12:23

the next input element we want to cover

play12:25

is going to be the date input element

play12:27

and again this is going to be just like

play12:29

all of our other input elements we're

play12:30

going to have a label which we're going

play12:32

to set up for to in our case we're going

play12:33

to say for date and we're just going to

play12:35

come in here and we're going to say this

play12:36

is going to be a birthdate for example

play12:38

so now let's go down here we're gonna

play12:40

create an input and this is going to

play12:42

have a type I just want to set this to

play12:44

date to specify it's a date I'm gonna

play12:46

make sure we set the name to date the ID

play12:48

to date so that we match our 4 for our

play12:50

label over here and we're just gonna

play12:52

close that off and now if we save you

play12:54

see that we get a date input which

play12:55

already has some defaults for us as well

play12:57

as a nice little date picker we can

play12:58

select what date we want inside of here

play13:00

just like with the number field we can

play13:02

for example specify a minimum and a

play13:04

maximum if we want so let's come in here

play13:06

and specify minimum which is going to be

play13:07

2019 June 10th let's save that and come

play13:12

in here and if I try to select the date

play13:13

before that it won't let me but it will

play13:15

allow me to select the day after that

play13:16

and if I come in here and manually

play13:18

change this for example I want to change

play13:19

it to the first and I try to submit it

play13:21

after making sure all the other fields

play13:23

are submitted properly make sure all the

play13:26

required ones are click Submit and

play13:27

you'll see that the value must be

play13:28

greater than that date that I specified

play13:30

as the minimum and again we could do

play13:32

maximum if we want but we already did

play13:33

that with numbers so we're going to skip

play13:34

that now let's move on to the next type

play13:36

of input which is going to be completely

play13:38

different than all the

play13:39

we've covered so far this is going to be

play13:40

the checkbox element so let's create a

play13:43

div and inside of this div we're just

play13:45

going to let's apply a little bit of

play13:46

text this is going to be outside of a

play13:47

label and this is just going to be for

play13:49

favorite food because we want checkboxes

play13:51

for all of our different favorite foods

play13:52

and now inside of here we're going to

play13:54

put all of our different checkboxes

play13:55

we're gonna do each one of these inside

play13:57

of their own div so firstly if we're

play13:59

gonna have a label which we need for all

play14:00

of our different elements we have a four

play14:02

and this first option that we want to

play14:04

have is going to be banana make sure we

play14:06

close that off put in banana here and

play14:08

then we actually want to create the

play14:09

input for that so this input is going to

play14:11

be a type here of checkbox

play14:13

we're going to specify here our name

play14:15

which is just going to be banana and

play14:18

we're going to specify lastly the ID

play14:20

which is again going to be banana close

play14:23

it off and save it you see we get a

play14:24

checkbox here for banana now let's say

play14:27

we want to add another favorite food

play14:28

let's just copy this div so that we can

play14:30

do the exact same thing but we just want

play14:32

to change this so we're going to change

play14:34

this to be Apple change the text to the

play14:36

Apple click and spell properly and make

play14:39

sure of course we update our name and

play14:41

our ID to be properly set now if we say

play14:44

that you see that we have banana and

play14:45

apple and we can check box both of this

play14:47

if we want or just one or none of them

play14:49

but what if we only want to be able to

play14:51

allow a user to check one box at a time

play14:53

instead of all of the boxes because with

play14:55

check boxes you can check as many or as

play14:56

little the boxes as you want but if you

play14:58

want to allow them only to specify one

play15:00

favorite food we need to use what's

play15:02

called a radio button so let's go down

play15:04

here and we're going to create another

play15:05

div and inside this one we're going to

play15:07

ask the user for gender because they can

play15:09

only have one gender and we're going to

play15:10

use a radio button for that so let's

play15:12

close this off here we're gonna create

play15:14

our div get a label inside of that and

play15:16

this label is going to be for our first

play15:17

one which is going to be male and we can

play15:20

just say male and then we need to create

play15:22

our input so the input is going to have

play15:24

a type here which is going to be radio

play15:25

and now you would be thinking that the

play15:27

name here should be male that's actually

play15:30

incorrect because these radio buttons

play15:31

all need to share the exact same name

play15:33

which is how we know that there can only

play15:35

be one selected so in our case this is

play15:37

actually going to be gender for our name

play15:39

and we want to also put in here our ID

play15:42

which is male which again corresponds

play15:44

with our for element for our label now I

play15:46

say that and you see we get a radio

play15:48

button for a male let's add the exact

play15:50

same thing for a female what's copied

play15:52

this

play15:52

down make sure that everything's

play15:54

indented properly and this is going to

play15:55

be female female and lastly our ID is

play15:59

going to be a female now if we say that

play16:01

and we click male that's all good but if

play16:04

we click female you see it unselect male

play16:06

and selects female for us and that's

play16:08

because we share the exact same name of

play16:10

gender between both our male label and

play16:12

our female label inputs down here you

play16:14

see they have the exact same name which

play16:16

means that we can only select one of

play16:18

them at a single time as opposed to be

play16:19

almost like multiple of that one thing

play16:21

that you need to do additionally with

play16:22

radio buttons though is actually specify

play16:24

a value so when you submit it to your

play16:26

form you know what you're getting back

play16:27

so in our case we want to put a value

play16:29

here which is going to be male and for

play16:31

female we want to put a value which is

play16:33

going to be female now I've saved that

play16:36

and actually enter in some information

play16:37

for our form so that we have some valid

play16:40

information and let's select male and we

play16:43

click Submit and you'll see we get

play16:44

gender here set to male which

play16:46

corresponds with the value that we

play16:47

selected here for our radio button

play16:49

now the next element that we want to

play16:50

look at is actually not an input element

play16:52

it's actually a select element but it

play16:54

works very similarly to the other input

play16:56

element let's go down here a little ways

play16:57

before our password and we want to come

play16:59

in here with our div and inside of this

play17:01

instead of an input like I said we're

play17:03

going to be using a select and of course

play17:05

we still want to label our select so

play17:06

we're going to make sure our label is

play17:07

specified here and inside of our select

play17:09

we're gonna select eye color so we can

play17:11

just say here our eye color and we want

play17:14

our label to say that eye color and

play17:16

inside of the select what we're going to

play17:18

do is we're actually going to put

play17:19

options inside of here so to do that we

play17:21

come down here we type an option and our

play17:24

option is going to have a value whoops a

play17:27

value so in our case we're going to put

play17:28

green as the value and it's also going

play17:30

to have that in here as the actual text

play17:33

so we can just put that here you could

play17:34

also use what's called the labels

play17:36

section and specify green here so we'll

play17:38

do that on another option was copy this

play17:40

down we're gonna do a red option so

play17:43

we'll say red but instead of putting our

play17:45

value inside the option like that we'll

play17:47

just say our label here is going to be

play17:49

red and also on our select we need to of

play17:52

course make sure we have a name here

play17:54

which we're just going to set to eye

play17:55

color and again our ID which is going to

play17:59

correspond with that label for that we

play18:01

have so ID here

play18:02

is going to be eye color now let's save

play18:05

that and you can see we have our

play18:06

selector which decides green and red as

play18:08

are two different values that we can

play18:10

select from and again we have green

play18:12

specified inside the option for the

play18:14

label or we can use the label attribute

play18:15

of our option either way allows us to

play18:17

set the actual value that we have seen

play18:20

inside of the select here now by default

play18:22

a select only allows you to select one

play18:24

option but you can come in here and you

play18:26

can use the attribute which is called

play18:28

multiple and if we say that we can now

play18:30

select multiple values by just holding

play18:31

down ctrl and clicking on them like this

play18:33

and now we have both of our values

play18:35

selected and if we fill in everything

play18:36

else on our form just so we can see what

play18:39

this looks like and makes our passwords

play18:40

there and we submit you see that has eye

play18:42

color showing up twice both for green

play18:44

and for red because we selected both of

play18:46

those values from our previous form now

play18:48

if we go back to our form you may notice

play18:49

that we don't have any form elements

play18:51

that allow us to specify lots of text

play18:53

and text with new lines in them and etc

play18:56

so in order to do that we're going to

play18:58

use another element wrap it inside of a

play19:00

div here and this one is called a text

play19:01

area and this again is not actually from

play19:04

an input it's its own element called

play19:05

text area and this is one of the few

play19:07

elements that has an actual closing tag

play19:09

because the value actually goes inside

play19:11

of the text area here as opposed to on a

play19:13

value attribute so now it's first before

play19:15

we get carried away put a label which is

play19:17

going to have four here and this is

play19:18

going to be a file for our user and we

play19:21

can just say bio and again we want to

play19:23

make sure we have an ID on here which is

play19:24

going to match the four from our label

play19:26

and a name so we can actually use this

play19:28

inside of our form and now let's put a

play19:30

bio inside of here so we're going to say

play19:32

my name is Kyle and if we save that you

play19:36

see that we have a bunch of extra space

play19:37

being added into our element here and

play19:39

that's because this text area converts

play19:41

all of the white space so all this white

play19:43

space before the my name is Kyle in this

play19:46

new line that we have here all that's

play19:47

being added in so if you have a default

play19:50

value for your text area you want to

play19:51

make sure there's no white space between

play19:52

the opening and closing tags now if we

play19:55

save this you said is properly not

play19:57

adding extra space force which is what

play19:59

we want but let's just remove that

play20:00

default value for now and we save that

play20:02

you see we can enter in our value as we

play20:04

want but we can also hit enter and do

play20:06

that and enter in multiple lines of text

play20:08

very easily and we can even expand this

play20:10

if we need to to make it larger to work

play20:12

with now select and text area are the

play20:14

only two anomaly type of elements that

play20:16

don't actually use the input for their

play20:18

element so now let's go back to the in

play20:20

to talk about another really interesting

play20:22

element which is called the hidden input

play20:24

element we don't even need to create a

play20:26

div for this because it's actually just

play20:28

completely hidden we won't even need a

play20:29

label we just need an input and you

play20:31

specify the type of hidden and that's it

play20:35

we're gonna give it a name so let's just

play20:37

say this is going to be called hidden

play20:38

and we're going to give it here a value

play20:40

and we're just going to set the value to

play20:42

high now if we say that you see nothing

play20:45

actually shows up on our form but if we

play20:47

do submit this form so let's make sure

play20:49

we submit it with all of our different

play20:50

required values and click Submit you see

play20:53

we get that hidden it attribute being

play20:54

passed along with the value we specified

play20:56

inside of our HTML but it doesn't

play20:59

actually show up on our page which is

play21:00

why it's a hidden input and users are

play21:02

not able to interact at all with hidden

play21:04

inputs which is exactly what they're

play21:06

used for they're really great when

play21:07

you're trying to do some fancy

play21:08

manipulation in JavaScript or you want

play21:10

to send something down from your server

play21:11

but if you're just creating a generic

play21:13

HTML form like this the hidden attribute

play21:16

is almost never going to be useful

play21:17

unless you're doing something fancy in

play21:19

JavaScript or on your server the next

play21:21

interesting element that we have to take

play21:23

a look at is actually going to be the

play21:24

file input so let's create a div so we

play21:26

can wrap this in here let's create a

play21:28

label for it first this is just going to

play21:30

be for what we'll just call file and we

play21:32

can just specify it as file then we're

play21:34

going to have our input tag we want to

play21:36

say that this is going to be an ID of

play21:38

file just so it has the same as our

play21:40

label the type here is it going to be a

play21:42

file type and again we're just going to

play21:45

give it a name a file so we can know

play21:46

what it's actually looking for when we

play21:48

submit our form and now you can see down

play21:50

here we can actually choose a file to

play21:51

submit to our browser but the first

play21:54

thing we need to do to make it so that

play21:55

when we submit this to a server that

play21:57

it's actually useful for our server we

play21:59

need to specify a new attribute on our

play22:01

form here and this is going to be called

play22:03

the E&C

play22:04

type just like this and inside of here

play22:06

what we want is multi-part form data and

play22:08

what this essentially says as files are

play22:10

very large we can't send them all at

play22:12

once it's going to send that file in

play22:14

multiple parts so we need to tell our

play22:16

server that we're sending in multi parts

play22:18

our form data into multiple different

play22:19

sections so that it knows to look for

play22:21

multiple sections of an upload as

play22:23

opposed to just one single form upload

play22:25

and this will actually allow us though

play22:27

can submit our files to our servers so

play22:30

now let's test that out let's come in

play22:31

here choose a file we'll just choose one

play22:33

of my channel arts

play22:34

type this in here make sure we have all

play22:36

the different information for our form

play22:37

submitted and if we click Submit you'll

play22:40

see our file is showing up as our

play22:41

channel art 1 PNG which is what we

play22:43

selected when we uploaded our file next

play22:46

let's kind of try to rapid-fire here

play22:47

through some of the lesser used input

play22:49

elements let's make sure we create our

play22:51

div and inside of this div we want to

play22:53

make sure we have our label and this is

play22:54

going to be for a phone we're gonna ask

play22:57

our user for their phone number so we'll

play22:58

say for phone gonna say phone is the

play23:01

label and we just want an input and the

play23:03

actual type of this is going to be tell

play23:05

te L just like this stands for telephone

play23:08

name here is going to be phone whoops

play23:12

phone cannot type today this is bed

play23:15

phone and an ID which is going to match

play23:18

our four of phone close that off and

play23:21

there we have our phone field and the

play23:23

really great thing about this phone

play23:24

field is that if a user is on a phone

play23:26

entering this information it'll give

play23:28

them a nice keypad which is specifically

play23:30

meant for entering phone numbers and not

play23:32

just for entering generic text next

play23:34

we're gonna have another input here

play23:35

which is going to be for URLs so let's

play23:37

create our div create our label for it

play23:40

which is going to say for URL close this

play23:43

off URL and we want to make sure we have

play23:46

our input which is going to have a type

play23:47

here of URL a name of URL and it's going

play23:51

to have an ID of URL close that off oops

play23:55

there we go and you can see we get our

play23:57

URL entering down here and another great

play23:59

thing about this again on mobile phones

play24:01

it's going to give them a special keypad

play24:02

but specifically meant for entering URLs

play24:05

lastly I want to cover just a kind of

play24:06

fun input element here which you

play24:08

probably won't ever see anywhere but

play24:09

it's just fun to use every once in a

play24:11

while this was going to be for color so

play24:13

we're gonna say here for color make sure

play24:16

it says color here and again as you

play24:18

guessed it this is going to be an input

play24:20

which is going to have a type of color

play24:22

whoops type color a name which is going

play24:26

to be color and it's going to have an ID

play24:28

which is color close this off and now

play24:32

you see here we have a color selector we

play24:34

can select any color that we want and

play24:35

click OK and it will actually select

play24:37

that color for us let's say we want to

play24:39

have this blue color click OK and it

play24:40

changes it to blue and finally after

play24:42

that long video we've covered everything

play24:44

you need to know about HTML forms make

play24:46

sure to click over here for more

play24:48

where I simplify the web for you and

play24:50

subscribe to the channel for more videos

play24:51

just like this thank you very much for

play24:53

watching and have a good day

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
HTML FormsWeb DevelopmentUser InputForm ElementsWeb DesignInput TypesForm ValidationWeb TutorialFrontendUser Interface
Benötigen Sie eine Zusammenfassung auf Englisch?