Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Working with number inputs

From: HTML5 First Look

Video: Working with number inputs

Email, URL, and search aren't the only new input types available for us in HTML5. In order to make building applications easier, the input form field now has mechanisms built into it to assist in inputting numeric values, from setting value ranges to choosing specific form controls, the number input type allows for greater control over submitting numeric data. So if you haven't already, go ahead and open up the trails.htm from 05_05 folder and I'm just again going to scroll through my code until I find our form.

Working with number inputs

Email, URL, and search aren't the only new input types available for us in HTML5. In order to make building applications easier, the input form field now has mechanisms built into it to assist in inputting numeric values, from setting value ranges to choosing specific form controls, the number input type allows for greater control over submitting numeric data. So if you haven't already, go ahead and open up the trails.htm from 05_05 folder and I'm just again going to scroll through my code until I find our form.

So just underneath the URL form input, we are going to place another input type. So I am going to go down the next line and the first thing we are going to do is just put in a label for this. So I am going to enter in a label tag and I am going to say that this label is for a form element called riders that we have not yet entered. Then inside of this label I am going to go ahead and just type in Number of riders. All right, below that we are going to place a new input element. Now for this input element I am just going to go ahead and do the type attribute first.

One of the things I want to talk about is notice that we have a number type and we are going to do that in just a moment, but notice we have the one directly underneath that as well which is range. Now if you use range you're going to get a slider bar in certain browsers and devices. This can allow the user to slide from one value to another. So that can be very handy for some applications. Now we are going to use the number one. So I am going to choose number and just type that in as attribute. Now number input types have some very specific attributes of their own. Let's go ahead and finish out our normal form attributes before we get into those.

So the first thing I am going to do is give it a name attribute of riders and an id attribute of riders as well. So just so we are in sync with everybody else on the page. And then we are going to go ahead and give it a tab index of 60 so that it again falls in sequence with the rest of our form. Now we are going to take a look at some of the specific values that we can set here. Notice for example that we have an attribute named step. Now we are not going to do step, but if you want the form to increment in certain value ranges-- For example, if we had a step of five, the initial value for the number will be 5 and then it would go to 10 and then it would go to 15 and so forth and so on.

So if you want to go ahead and restrict it to certain values, step is a really nice way of doing that. But something that we also have are minimum and maximum values. Now we are not going to set a maximum value, but we could do that by typing in max. If they went riding with a hundred riders, we kind of want to know about that. So we're not going to go ahead and put a maximum value in here. We are however going to do a minimum value. We are going to say you had to have at least one rider on this trail with you. So we are going to go ahead and do a minimum value of 1. That way, depending upon how the form controls are exposed, and in some cases you're just going to get a text box.

In other cases, you're going to get a stepper. So if you get that numeric stepper, it won't allow the user to step it below the number 1, which is nice. It's not restricting what somebody could type in. It's not restricting what they could submit, but it is restricting the form controls that might result from this particular type of form element. Now just after that I am going to type in a default value for this as well. If you wanted to start it off at a certain value, you could go ahead and do that and we are going to start this off at 1 as well, because more than likely it was a single rider, but maybe there were five in the group or ten in the group.

In this case, if it was just one rider, he or she is not going to have to keep typing that in. It will already be in the form element for them. Okay, cool! So now there is our input form element. We are done with that. So we'll go ahead and close that out. Let's save it and let's test it. This is one of the ones where you're going to see a pretty dramatic difference based upon which browser or which device you test this in. So I am going to go ahead and test it first in Chrome. In Chrome if I go down to Number of riders, notice it's already populated to 1, but there is no real form controls. So there is nothing for it to stop me from typing in 0 or even -3.

So I don't have any form controls here and we don't really have any stepping. So Chrome isn't exposing any form controls outside of our normal text field. We see in Safari, we do get a stepper. Notice we cannot go below 1 and unfortunately it won't let us go up either. So although Safari does expose the controls, the controls really aren't doing anything for us. We still have to type those in. So at least with Safari we see the genesis of what these form elements will eventually look like. Let me just try something real quick here guys. This is just for me, this is not for the users. Now if I test this in Firefox, again notice that just like Opera, I just get a normal text field, no exposed controls there whatsoever.

However, if I test this in Opera, notice again we get some form controls, and now those form controls are working. So you'll notice that I can use the actual stepper controls to step up and I can use them to go ahead step down. However, I can't go past one rider. So if I just click down right now, nothing is happening whatsoever and that's the minimum value coming into play. So Opera gives you a little bit more robust set of form controls, but it should give you some idea as to how other devices and browsers in the future will go ahead and represent these numeric steppers. Now having more controls at your disposal is a great thing.

By using these different types of numeric inputs you can create forms that conform a little bit more closely to your desired input. It's worth pointing out here as well that the HTML5 specification also contains some new JavaScript methods to deal with these particular types of controls. Of particular note would be the input value as number method. So it's written as input.value as number. Value as number method, which will convert the string data type in any numeric form control to a floating point number for you and that's going to help you make calculations and working with form data a little bit easier within your scripts.

So we are almost done with our form, but I want to talk about one more new form element and we will do that in our next movie when we talk about date pickers.

Show transcript

This video is part of

Image for HTML5 First Look
HTML5 First Look

50 video lessons · 73813 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 3m 56s
    1. Welcome
      1m 1s
    2. Using the exercise files
      1m 50s
    3. Who is this course for?
      1m 5s
  2. 21m 12s
    1. Exploring prior standards
      4m 26s
    2. Why do we need HTML5?
      3m 32s
    3. HTML5 timeline
      4m 24s
    4. Current HTML5 support
      4m 25s
    5. What HTML5 is (and what it isn't)
      4m 25s
  3. 27m 49s
    1. HTML5 vs. HTML4
      3m 25s
    2. New structural tags
      6m 1s
    3. New content tags
      4m 7s
    4. New application-focused tags
      5m 32s
    5. Deprecated elements
      4m 28s
    6. API overview
      4m 16s
  4. 22m 29s
    1. Content models
      5m 33s
    2. Understanding the outline algorithm
      3m 21s
    3. The role of ‹div› tags
      4m 20s
    4. Using ID and class attributes
      2m 6s
    5. DOCTYPE declarations
      4m 16s
    6. Character encoding
      2m 53s
  5. 41m 27s
    1. Basic page structure
      3m 40s
    2. Structuring top-level elements
      7m 30s
    3. Structuring interior content
      8m 42s
    4. Building headers
      9m 11s
    5. Checking document outlines
      5m 46s
    6. Ensuring cross-browser structure
      6m 38s
  6. 27m 53s
    1. New input types
      5m 57s
    2. Setting form autofocus
      2m 53s
    3. Using placeholder data
      4m 4s
    4. Marking required fields
      3m 24s
    5. Working with number inputs
      5m 49s
    6. Using date pickers
      5m 46s
  7. 1h 1m
    1. Canvas overview
      6m 21s
    2. Adding canvas content
      8m 58s
    3. Drawing in the canvas environment
      12m 9s
    4. Drag-and-drop API overview
      6m 18s
    5. Offline applications overview
      7m 11s
    6. Video overview
      5m 45s
    7. Encoding video
      8m 23s
    8. Adding video
      5m 58s
  8. 57m 33s
    1. Geolocation API overview
      5m 50s
    2. Web storage API overview
      5m 40s
    3. WebSockets overview
      4m 16s
    4. CSS3 overview
      6m 38s
    5. Enhancing typography with CSS3
      7m 42s
    6. Using @font-face
      7m 11s
    7. Styling HTML5 with CSS3
      10m 23s
    8. Using CSS3 transitions
      9m 53s
  9. 5m 6s
    1. Final thoughts
      3m 49s
    2. Goodbye
      1m 17s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5 First Look.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.