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

Adding required fields and placeholders

From: Validating and Processing Forms with JavaScript and PHP

Video: Adding required fields and placeholders

One of the most helpful HTML5 atrributes for form validation is the required attribute. On browsers that support it, it will prevent the form from being submitted unless the input is filled out. I'll be using this form from our exercise files. This is one of the forms I styled in my course on CSS styling forms. If we take a look at the code for this page, you can see a single input field. I'm going to add the required attribute to the input field and then switch over to the browser. I'll refresh the form. And if I try to submit it without filling it out, I'll get a little error message that says please fill out this field. It also won't let me submit the form.

Adding required fields and placeholders

One of the most helpful HTML5 atrributes for form validation is the required attribute. On browsers that support it, it will prevent the form from being submitted unless the input is filled out. I'll be using this form from our exercise files. This is one of the forms I styled in my course on CSS styling forms. If we take a look at the code for this page, you can see a single input field. I'm going to add the required attribute to the input field and then switch over to the browser. I'll refresh the form. And if I try to submit it without filling it out, I'll get a little error message that says please fill out this field. It also won't let me submit the form.

When you use the required attribute, it's always a good idea to also use the placeholders attribute. Placeholders give users a hint as to how they're supposed to fill out our field. That way, the user will know that I don't simply want their first name. Let me refresh this page. And you can see that it appears as a grayed out item. As soon as I start typing something in here, it will disappear. It's always a good idea to give users feedback with things like required fields and placeholders, but you shouldn't rely totally on this type of validation because not all browsers support it. A good place to check is the website caniuse.com.

You can check for support for any HTML5 feature by doing a search. You can see that the required parameter only works on newer versions of IE and none of the mobile browsers. To learn more about forms, make sure you check out HTML5 Web Forms in Depth, with Joe Marini.

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.