New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

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

Constraining numeric and date fields

From: Validating and Processing Forms with JavaScript and PHP

Video: Constraining numeric and date fields

Some numeric fields have attributes that let you control the number of characters as well as the minimum and maximum value. These will even work with dates. So, I have a simple form here, where I'm asking the user a name, age, and when they would like to make a reservation. You can have the browser prevent the user from typing fields longer than a certain length. With a maxlength attribute that works for fields like text, email, search, password, telephone or url. We can for example make sure that their name is no longer than ten letters, perhaps our database limits the length of that field. We'll see how that works, we'll go into our code for this page and modify the name input field, so that it has a maxlength we'll make it ten characters. So I'll save this, I'll go back to my form, Refresh, and when I try to type in something longer than ten characters, the browser will stop me at ten characters. Some other attributes that you can use are min and max. They let you create constraints to numeric input fields. Say for example that you want to add an age constraint to a form element, so that someone has to be at least 13 years of age to submit this form. I'll switch back to my code, and under the age input field, I'll add a min value of 13.

Constraining numeric and date fields

Some numeric fields have attributes that let you control the number of characters as well as the minimum and maximum value. These will even work with dates. So, I have a simple form here, where I'm asking the user a name, age, and when they would like to make a reservation. You can have the browser prevent the user from typing fields longer than a certain length. With a maxlength attribute that works for fields like text, email, search, password, telephone or url. We can for example make sure that their name is no longer than ten letters, perhaps our database limits the length of that field. We'll see how that works, we'll go into our code for this page and modify the name input field, so that it has a maxlength we'll make it ten characters. So I'll save this, I'll go back to my form, Refresh, and when I try to type in something longer than ten characters, the browser will stop me at ten characters. Some other attributes that you can use are min and max. They let you create constraints to numeric input fields. Say for example that you want to add an age constraint to a form element, so that someone has to be at least 13 years of age to submit this form. I'll switch back to my code, and under the age input field, I'll add a min value of 13.

I'm going to save that and go back into our page. I'll refresh my form. And now if I try to type in a value like nine. When I try to submit the form, it's going to tell me that the value has to be greater than or equal to 13. In the same way, the max field works to limit fields to a certain number. So I'll type in a max of 18. I'll save this. Back into our form, refresh, and now try to type in something larger than 18. Try to submit and I get a similar error. Now the cool thing about min and max is that you can control things like dates and date time. So, for example, if we wanted to limit the date for our reservations to only dates within the current work week, we could use a min and max value for that. So, let's see how that works.

I'm going to go back into my code and under the date input field I'm going to type in a minimum and a maximum value. I'll save this and I'll go back into my form, and I'll refresh. When I click on this input field, the calendar popup will only let me select dates within that time range. You can't always guarantee that the browser you're using is going to implement these attributes, so make sure you check out a website like caniuse.com and look for those attributes by typing them in. You may also want to test your forms in older browsers.

Show transcript

This video is part of

Image for Validating and Processing Forms with JavaScript and PHP
 
Expand all | Collapse all
  1. 3m 36s
    1. Welcome
      1m 9s
    2. What you should know
      1m 2s
    3. Using the exercise files
      1m 25s
  2. 12m 50s
    1. Understanding forms
      2m 2s
    2. Working with form fields
      7m 4s
    3. Using the form tag
      3m 44s
  3. 19m 23s
    1. Using input validation
      2m 10s
    2. Adding required fields and placeholders
      1m 31s
    3. Constraining numeric and date fields
      2m 32s
    4. Accepting multiple entries
      1m 41s
    5. Limiting uploads by MIME types
      2m 35s
    6. Assisting text input with a datalist
      1m 55s
    7. Constraining with regular expression patterns
      6m 59s
  4. 42m 13s
    1. Accessing forms
      3m 57s
    2. Looking up form elements
      3m 35s
    3. Handling focus changes
      2m 47s
    4. Detecting the onchange event
      4m 31s
    5. Using the selectedIndex property
      2m 30s
    6. Dynamic validation with regular expressions
      7m 0s
    7. Creating a generic input validation function
      4m 31s
    8. Validating in older browsers with Modernizr
      7m 32s
    9. Interrupting form submission with onsubmit
      5m 50s
  5. 15m 20s
    1. Understanding jQuery
      3m 47s
    2. Validating on submit with jQuery
      3m 45s
    3. Building interactive jQuery validation
      2m 34s
    4. Using the jQuery Validation plugin
      5m 14s
  6. 32m 57s
    1. Communicating with PHP servers
      2m 27s
    2. Retrieving data from superglobals
      8m 18s
    3. Using server-side validation
      4m 59s
    4. Adding in-page validation
      5m 22s
    5. Mirroring input data back to the user
      7m 46s
    6. Sanitizing form input
      4m 5s
  7. 43m 29s
    1. Mailing form data
      8m 28s
    2. Understanding file uploads
      3m 1s
    3. Uploading files
      9m 20s
    4. Processing form data with AJAX
      8m 14s
    5. Preparing your database
      5m 50s
    6. Pushing data
      8m 36s
  8. 1m 17s
    1. Next steps
      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

Are you sure you want to delete this note?

No

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.