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

Validating in older browsers with Modernizr

From: Validating and Processing Forms with JavaScript and PHP

Video: Validating in older browsers with Modernizr

If I had a time machine, I would go back in time, and do something about Internet Explorer's 6 through 9. They create all kinds of problems, and JavaScript is no exception. Here I am on Internet Explorer 9, and I wanted to show you that the validation that I've built, is not working. So, if I type something in here, and I hit the Tab key, you can see that it's not recognizing any validation. So, let's see what's happening. I'm going to go to the Developer tools. I can do that by clicking on this menu, and selecting Developer tools. From here you want to make sure that you click on Script, and then on the right hand side, make sure that you click on Console.

Validating in older browsers with Modernizr

If I had a time machine, I would go back in time, and do something about Internet Explorer's 6 through 9. They create all kinds of problems, and JavaScript is no exception. Here I am on Internet Explorer 9, and I wanted to show you that the validation that I've built, is not working. So, if I type something in here, and I hit the Tab key, you can see that it's not recognizing any validation. So, let's see what's happening. I'm going to go to the Developer tools. I can do that by clicking on this menu, and selecting Developer tools. From here you want to make sure that you click on Script, and then on the right hand side, make sure that you click on Console.

If you want to, you can pull up JavaScript from the page, clicking on this pop up menu. So, on the right hand side at the bottom, I'm going to ask the browser to show me the place holder. So, I'll type in document, my form, my name and then placeholder. And notice that it's not showing me anything. That means that this browser doesn't recognize the placeholderb as a value for that input field. The weird thing is, that if I switch this browser to IE8 mode, so I'm going to do that by going right here, and switch into Internet Explorer 8. And switch the document mode to also Internet Explorer 8, it's already chosen. And right here on the console, I'm going to hit the Up arrow. That brings up the last command, and then I'm going to hit Enter. Now it's showing me the value for the placeholder, which is last comma first. And as a matter of fact, if I type something in here, say my name again, and I hit Tab, it recognizes my validation.

I verify that this is what happens, on a real IE 8 browser. And it's one of the problems that comes up, when working with older browsers. It doesn't really make any sense, for the pattern attribute to be recognized in IE 8, but not on the newer IE 9. This is a definitely a problem, that we're going to need to address. So, in order to fix that, I'm going to use a library called modernizer. modernizer let's you look for feature support in different browsers. We can use it to detect whether or not a browser has support for patterns, and placeholder attributes, and then add some backup validation for those. It's super easy to use and install.

So, from the Home page, if you just want to play around with it, you can click on Download This Development Version. But notice it's 42k, and we don't need the whole library, we just need the part that deals with forms. So, if you click on Download, you can figure your Own Build, and customize how the library will get downloaded. We really don't need very much, so I'll click these Off right here. And I'll make sure that I have the part of the library that checks for input attributes, and input types only.

Now, from here I can click on this Generate button, and it's going to generate a special version of the file for me to Download. So, I'll click on this Download Custom Build, and it's going to Download that, and let me go ahead and Open this up. So, I'm going to look for it in the Downloads folder, and here's my customized JavaScript file. Now, I'm going to Rename it modernizer_forms.js, and then put it into my projects folder. Now I'll go back into my code, and in the index, that HCML file, I need to load that library.

I'll go to the very top, and right underneath my styles, I'm going to add a Script Tag, and the Script's going to have a Source of modernizer_forms.js. So, it loads that library at the beginning, and I'm going to Save that. That's all I need to do on this page. Next, I'll go into myscript.js, and I'll start creating an alternate way for browsers that don't support those fields, to have validation. So, right underneath my input fields, I'm going to need to create a JavaScript Object. If you want to learn more about JavaScript Objects, make sure you check out my course JavaScript and JSON and online training library.

So, what I'm going to do here is, create an Object called Validation Info. It's going to be in a new variable. This Object is going to have information, that's going to be similar to the information already on the form. So, I'm going to go here and type in my name, that'll create a My Name Object, and that will have another Object inside it. And in here I'll type in pattern, and place a pattern for the name field. Then I'm going to use another item called a Placeholder, and this will have the text for my Placeholder. So, we need to get this pattern.

I'm going to go back into my index, that HTML file, and I'll find the area with the name and Copy the pattern from right here. So, I'll Copy this, go back to my Script, and Paste that right there. And now I'm going to add another Object, so the things that I want to validate on this form, are gong to be my name, as well as my telephone. My telephone is going to have a different pattern, and a different Placeholder. And I'll go back into my index, that HTML file, and I'll Copy the pattern from here as well. Now I need to go ahead and Escape the slashes, so I'll just type in double slashes, for all the single slashes here, to fix my pattern. Now, right now, here's the part of the code where I'm checking for a pattern, as well as a Placeholder, that are coming right now from the input fields in my form.

I need to Modify that, and use that, only when the pattern attribute is available to the browser. So, this is where I need to use modernizer. modernizer creates an object called modernizer, that has information about each type of thing, that you're trying to check for. So, what we need to do here is create an If statement, check the modernizer object, for input field in pattern. So, if the modernizer object has a pattern field in the input object, than I can do something, and what that's going to be is, what I was doing before. This is what the browser will use when there's normal pattern support. Otherwise, I need to do something else, and this is where my object comes in. It's going to be pretty much what I did before, except that instead of using this pattern, it's going to use my object that I created up here, called Validation Info.

And I'm going to pass it, my name or the name of the current field, remember, this is a loop that goes through all the fields in the form. So, this should say this, dot name, an then it's going to look for a pattern. And that pattern would be this field right here, or this field right here, as it goes through the form. And then for Placeholder, it's pretty much going to be like this, except it's going to look for the Placeholder object.

The rest of the Script is going to be pretty much the same. Here I am back on Internet Explorer 9, but I'm still running on IE 8 mode. So, let's switch over to IE 9 mode. I'll go to Browser mode here, and select IE 9. Make sure that my document mode is also in IE 9 standards. And I'm going to see if my changes work. And sure enough, adding modernizer has made it possible for our form, to understand our validation even in IE9, which wasn't supporting the Placeholder standard before. So, I'm going to go ahead and Save this, and this should be working now, in old versions of Internet Explorer, namely, Internet Explorer 9. So, old versions of IE require a little special tender loving care. modernizer makes it easy to detect for shortcomings, in older browsers, so that we can write code, that is more compatible and accessible, to a larger part of the net population.

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
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 Validating and Processing Forms with JavaScript and PHP.

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.