Creating Responsive GUIs with Real-Time Validation | Page 3 | WebReference

Creating Responsive GUIs with Real-Time Validation | Page 3

[previous] [next]

Creating Responsive GUIs with Real-Time Validation

Putting Together the Validator Application

Aside from the standard toolkit JavaScript files, including validatekit.js, the Validator application consists of only two components:

  • HTML Web page (validator.html)
  • PHP server script (ziplookup.php)

Of course, the other major ingredient in the application is the third party location Web service that is responsible for providing the city/state data based upon a ZIP code. Although this service is an important part of the application, it doesn't require a distinct component other than code within the HTML Web page that initiates an Ajax request and code within the PHP server script that handles relaying the response back to the page. This arrangement is necessary because Ajax requests aren't allowed direct access to resources located on other domains.

The next couple of sections explore the Validator Web page and server script in detail in order to fully understand how the application works.

The Validator Web Page

The HTML Web page for the Validator application, validator.html, consists of a series of text box input fields, each with a non-input text field next to it that serves as a display area for help messages. The text boxes are created as input elements, while the help message fields are created as span elements. Figure 7.4 shows the Validator application open in a Web browser with the blank text fields ready for user input and all of the help messages indicating that the fields are missing data.

Note: The application doesn't automatically display help messages initially as shown in the figure. Instead, the "Please enter a value." help message does-n't appear until you actually navigate away from an input control without entering anything.

With the layout of the Validator application in mind, as revealed in the figure, check out the HTML code for the body of the Web page:

After the focus is set to the first text box on the page via the onload event handler, the body of the page moves on to creating each of the text box input and associated help message span pairs. There isn't much particularly unusual about any of this code except for the onblur event handler, which gets called when the input focus leaves an input control. So, when the user tabs away from a text box or clicks somewhere else on the page, the text box receives an onblur event notification. This is where you call a function to validate the input value in the text box.

The functions that take care of the validating in the Validator application are all part of the validatekit.js Ajax Toolkit file, which must be imported into the Web page with the following line of code (placed in the head of the page):

Each of the functions provided by this file accept the same two function parameters:

  • The input control containing the value to be validated
  • The help control used to display help text

To see how these parameters get passed into a validation function, take a look at the code that calls the integer validation function:

Because the function is called from within the context of the integer text box, you can pass this as the first parameter to the function. The second parameter is then specified using the standard getElementById() function to grab the integer help control, whose ID is integer_help.

If you pay close attention to the help controls on the page, you may notice that each of the span elements is styled to a CSS style class named help. This style class simply changes the color of the font to red (#FF0000) and the style of the font to italic, which makes the help messages jump out a little more on the page. Following is the code for the Validator Web page's internal style sheet, which includes the style class:

A global div style is included in the style sheet purely to help provide a bit of vertical spacing between the input controls on the page.

Although the Validator internal style sheet is helpful in making the page look a little better, it doesn't do much to help on the Ajax front. The code that initiates Ajax requests for the Validator application is contained in the onblur event handler for the ZIP code text box. Although this code is in the listing you just saw, it's easier to follow if you break it out of the onblur attribute:

This code first validates the ZIP code input data to see if it is indeed a five-digit number. This is accomplished with the call to the validateZipCode() function. The return value of the function indicates whether the data is valid (true) or invalid (false). If the data is valid, the getCityState() function is called to initiate an Ajax request. Here's the code for this function:

Ah, you finally see some of the magic that makes this application look cool. The first task in the getCityState() function is to display the animated "loading" image, wait.gif, in the ZIP code help control. This image will remain displayed until the Ajax request is completed, thereby giving the user a visual cue that something is taking place behind the scenes.

The second task in the getCityState() function is to actually submit the Ajax request, which involves packaging the ZIP code into a URL for the ziplookup.php server script. The handleCityStateRequest() function is specified as the request handler for the ZIP code Ajax request. Here's the code for it:

The job of this function, which is called when the Ajax request completes, is to extract the city and state from the response data and display them on the page in place of the "loading" image. The city/state data is extracted with a little help from the getText() Ajax Toolkit function, which is located in the domkit.js file. Notice that the XML element names CITY and STATE are used as the basis for accessing the city and state data. Not only is this data extracted, but it is carefully reformatted using HTML code constructed on the fly. If all goes well, the resulting HTML code looks something like this:

Notice that an inline style is applied to the city and state so that they appear in a bold font, which helps to make them distinguishable from normal help text. If there is a problem with the XML data returned from the server, a help message is displayed indicating that the ZIP code could not be found.

That wraps up the code for the Validator Web page. All that's left is to take a quick look at the PHP server script responsible for passing along the ZIP code request to a remote server.

[previous] [next]