Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 68 69 70 71 72 73 < 74 > 75 76 77 78 79 80 .. 118 >> Next

212 Part III: Making Your Site Easy for Visitors to Navigate and Use
Part IV
Interacting with Users
The 5th Wave By Rich Tennani
<5RW|Enn<w\ — —--------------------
“You’re part o£ an ‘Insect-Clothing Club’ on the Web? Neat' "Where do you get buttons that small ?”
In this part . . .
M^art IV is jam-packed with information for making pro* fessional-looking Web pages that are so cool you just might shock yourself! Chapter 12 shows you how to gather and verify input from the folks who visit your Web site — including time-tested tips to help you design user-friendly Web pages and communicate effectively with your users. In Chapter 13, you see how to turn a simple Web page into a Web-based application by hooking your script to a user-initiated event, such as key press or a mouse click. And finally, Chapter 14 introduces you to JavaScript error-handling techniques that you can use to replace generic error messages (which can frustrate your visitors) with specific, appropriate, user-friendly error messages.
Chapter 12
Handling Forms
In This Chapter
^ Getting information from your users ^ Verifying user input ^ Giving your users helpful feedback
f
■ f you’re familiar with HTML fill-in forms, you know how useful they can be. Adding an HTML form to your Web page lets your visitors communicate with you quickly and easily. Users can enter comments, contact information, or anything else into an HTML form. Then that information is transmitted automatically to you (okay, technically, to your Web server) the instant your users submit the form.
Although HTML forms are great all by themselves, JavaScript makes them even better! By using JavaScript, you can create intelligent forms — forms that instantly correct user input errors, calculate numeric values, and provide feedback. In developer-talk, what JavaScript gives you is a way to perform client-side data validation (sometimes referred to as data scrubbing), which is an essential component of any well-designed piece of software, from simple Web page to full-blown online application.
Capturing User Input by Using HTML Form Fields
JavaScript adds two very useful features to plain old HTML forms:
Ii^ JavaScript lets you examine and validate user input instantly, right on the client.
JavaScript lets you give users instant feedback.
216 Part IV: Interacting with Users
I explain both of these features in the following two sections.
Creating an input-Validation script
Back in the old days, Web developers had to write server-side Common Gateway Interface (CGI) programs to process user input. That approach, which is still in use, is effective — but inefficient.
For example, imagine that you want to allow your visitors to sign up for your monthly e-newsletter, so you create an HTML form containing a single input field called E-mail Address. Then imagine that a visitor accidentally types XYZ into that field (instead of a valid e-mail address such as janedoe@aol.com). The contents of the E-mail Address field have to travel all the way from that user’s Web browser to your Web server before your CGI program can examine the information and determine that XYZ is invalid.
By using JavaScript, on the other hand, you can instantly determine whether an input value is valid, right inside the user’s browser — saving the user valuable time. (And saving yourself the trouble of having to figure out how to create a CGI program in C, C++, or Perl!)
Different strokes for different folks: Data validation using regular expressions
Writing scripts is like anything else in life: Usually, more than one way exists to approach any given problem. Some JavaScript programmers like to spell things out much the way I demonstrate in the code that you see in this chapter — in other words, to use as many lines of script as necessary to create a human-readable, working script. Other JavaScript programmers sacrifice human readability for brevity, reasoning that fewer lines of code means fewer lines to debug.
For those of you in the latter camp, regular expressions can come in mighty handy. A regular expression is a special kind of pattern that you can use to specify text strings. For example, here's a regular expression that describes a somebody@someplace.some_suffix e-mail address:
/A\w+@\w+(\.\w{3})$/
Scary stuff! But when you break it down into little pieces, you understand how it works, as you can see in Table 12-1.
Chapter 12: Handling Forms 217
Figure 12-1:
Hey, that’s not an e-mail address!
Input validation generally falls somewhere in one of the following three categories:
Existence: Tests whether a value exists.
Numeric: Ensures that the information is numbers only.
Pattern: Tests for a specific convention, such as the punctuation in a phone number, an e-mail address, a Social Security number, or a date.
In Listing 12-1, you see the JavaScript code required to validate the oh-so-common pattern category: an e-mail address. (The order form script section in this chapter demonstrates examples of existence and numeric validation, as well as pattern validation.)
Previous << 1 .. 68 69 70 71 72 73 < 74 > 75 76 77 78 79 80 .. 118 >> Next