From the course: Validating and Processing Forms with JavaScript and PHP
Unlock this course with a free trial
Join today to access over 22,400 courses taught by industry experts.
Detecting the onchange event
From the course: Validating and Processing Forms with JavaScript and PHP
Detecting the onchange event
One of the most important events for validating and processing forms, is the onchange event. It triggers when the content of a form element changes. This is different than onfocus and onblur, which fire when a field goes in and out of focus. With validation, sometimes you'll need to use different types of events, so it's nice to know that these can work well together. Let's take a look at how we can use these events for validation. So I have a simple form and it just has a URL field. I want to make sure that if I have a URL, that at least it starts with an http://. So I'm going to be using the onchange method, and here's my HTML. Can see that I've placed a span right here to accept errors for validation. So I'm going to stat by creating a script tag. And in my script bag, I'm going to target the My URL field. So, I'll do document, the form, then get to the My URL field and then add the onchange handler. This is going to execute a function literal, which means just a function without a…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Accessing forms3m 57s
-
Looking up form elements3m 35s
-
Handling focus changes2m 47s
-
Detecting the onchange event4m 31s
-
Using the selectedIndex property2m 30s
-
Dynamic validation with regular expressions7m
-
Creating a generic input validation function4m 31s
-
Validating in older browsers with Modernizr7m 32s
-
Interrupting form submission with onsubmit5m 50s
-
-
-
-
-