Books
in black and white
Main menu
Home About us Share a book
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 .. 73 74 75 76 77 78 < 79 > 80 81 82 83 84 85 .. 118 >> Next

// The following if statement checks all the pieces,
// like so:
// if openParen is not equal to "("
// OR the areaCode is not a number // OR the closeParen is not equal to ")”
// ... and so on.
if (
openParen != "(") !isANumber(areaCode)) closeParen != ")”) !isANumber(exchange)) dash != "-”) !isANumber(line))
alert("Incorrect phone number. Please re-enter in the following format: (123)456-7890”)
}
}
}
<FORM NAME=”feedbackForm”>
<BR>Please enter your home phone number <BR>in the following format: (123)456-7890 <INPUT TYPE=”text” NAME=”homePhone” VALUE=”” SIZE=13 onBlur=”isAPhoneNumber(this.value)”>
)
230 Part IV: Interacting with Users
Giving 'em a piece of your mind
Giving users appropriate, timely feedback can be the difference between a confusing Web site and one that is efficient and pleasant to use. The following are a few things to keep in mind as you decide when and how to interact with your users.
DON'T SHOUT!! Nobody likes being yelled at, and messages THAT ARE IN ALL UPPERCASE LIKE THIS AND END IN EXCLAMATION POINTS ARE YELLS! Say what you need to say; just use normal capitalization and punctuation.
In general, be specific. Sometimes, you don't particularly care what a user types (for example, if you're asking for free-form comments on your product). At other times, what the user types is crucial. For the times when it's crucial, be sure to let the user know up front, right on the page, what format is expected.
When you do need to pop up an error message, make sure that it tells users precisely what's wrong with their input. (Invalid format. Please retry. doesn't count!)
Give your users a break. Just because you're now a card-carrying expert at validating user input doesn't mean you have to pop up an error message every time you detect an error. In some
cases, you might be able to massage (geekspeak for modify) the input data to suit yourself without bugging the user at all. For example, just because you'd like to see a value in uppercase letters doesn't mean the user has to enter it in uppercase letters. Instead of displaying an error and requesting that the user retype the entry, for example, you can just as easily take the input and change it to uppercase yourself using the toUpperCase() method of the string object.
Pat your users on the back. Don't reserve feedback for only those times when a user enters something incorrectly. Reassuring users that things are proceeding as planned is just as useful. For example, let users know when a form passes all validation checks.
Test 'til you drop. Make sure (and this should go without saying, but you never know) that you test your form carefully for every conceivable error (and series of errors) that a user might reasonably be expected to make. Few things are more frustrating to users than getting tangled in an endless loop of errors that refuse to go away, even after the user has figured out what's wrong and corrected it!
A better approach for dependent field validation is to wait until users try to submit their forms before executing your validation scripts, as shown in Listing 12-7. Now, if the user attempts to submit a form without entering either a phone number or an e-mail address, the script generates an error and prevents the form from being submitted. To see how this code behaves at runtime, take a look at Figure 12-4.
Chapter 12: Handling Forms 231
Figure 12-4:
Using form-level validation to ensure that at least one option is filled.
0} • "ulei foim ex.impie (front JavaScript For Diminues. ttli édition} Mi< rosoft Internet Explorer
; File Edi! View F.ivorites Tools Help
: QBack * © ' 3 L“l J^Sutth "^Favorites Media ^ t2w * □ ша©
; Address C:\ern\wrKe\Jsfd4e\scripts\listl206.htm \ш
Microsoft Internet Explorer a
Is there anything else уоі
■\ Please let us know whether youd like us to contact you by e-mail or by
» \ phone. Thanks!
OK □
V| 1
Your first name: L ist name:
Eileen Dover
How would you like us to contact you (e-tnail. phone, or both)?
e-mail □
telephone LJ
I Submit your quote request ) [ Clear the form ]
^ My Compute»
Ед TextPad - [C:\em Й] 506331 Chl2j.d. .
[§) tie Bat1
5:3)
Listing 12-7: Implementing Dependent Validation Checks with the validateForm() Function
<HTML>
<HEAD>
<TITLE>Order form example (from JavaScript For Dummies, 4th Edition)</TITLE>
<SCRIPT LANGUAGE=”JavaScript” TYPE=”text/javascript”>
<!-- hide this script from non-javascript-enabled browsers
////////////////////////////////////////////////////////////
// Checks to see whether a value contains non-numeric data. //////////////////////////////////////////////////////////// function isANumber(inputValue){
// Assume everything is okay. var result = true
(continued)
232 Part IV: Interacting with Users______________
Listing 12-7 (continued)
// If parseFloat() returns false, a non-numeric // character was detected in the first position, if (IparseFloat(inputValue)) { result = false
Previous << 1 .. 73 74 75 76 77 78 < 79 > 80 81 82 83 84 85 .. 118 >> Next