Bring Your Forms to Life With Javascript | WebReference

Bring Your Forms to Life With Javascript

By Kris Hadlock


[next]

Digg This Add to del.icio.us

In this article we'll take a look at how to prevent errors in your forms and ultimately bring them to life through JavaScript. The source code for the samples used in this article can be downloaded here and a live example can be viewed here. Now let's add some life to those old forms.

Creating a Basic Web Form

We'll start by creating a basic registration form with a few common form elements. We'll create an HTML page and add the the registration form code from listing 1, shown below.

The code from listing 1 will allow us to post data to a server and register a user. It could also display any errors or warnings based on server-side results, but this is equivalent to asking your users to complete a form and hope that it will be sent successfully. There's really no reason to make a user wait when we can provide them immediate feedback and prevent issues from occurring rather than having to deal with them after the fact. Having said that, it would be useful to have a JavaScript object that allows us to validate all our forms without having to rewrite code everytime we create a new form. Let's take a look at how to accomplish this by creating a reusable JavaScript validation object and tying it to the form we just created.


[next]