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 .. 72 73 74 75 76 77 < 78 > 79 80 81 82 83 84 .. 118 >> Next

}
}
The i sANumber() function definition uses the built-in JavaScript function parseFloat() to weed out all values beginning with something other than a number. (The parseFloat() function returns a value of NaN if the first character that it encounters can’t be converted to a number.)
Chapter 12: Handling Forms 227
In the event that the first character is a number but subsequent characters aren’t (for example, to catch a mistake like 5F5-1212), isANumber() steps through all the remaining characters in inputValue to see whether it can detect a non-numeric character.
The last few statements in Listing 12-5 show you an example of how you can call the isANumber() function. In this example, the isAValidPhoneNumber() function (which you get to examine in detail in the next section) calls the isANumber() function as part of its own validation routine.
Testing for patterns
Listing 12-1, shown previously in this chapter, demonstrates how you might go about validating a very common pattern: the e-mail address. Here, you see an example of another common use for pattern validation: making sure a user types a valid telephone number. Listing 12-6 shows you what I mean.
Listing 12-6: Validating a Phone Number
//Defining the isAValidPhoneNumber() function function isAValidPhoneNumber(inputValue) { var digitsFound = 0
// Step through the inputValue to see how // many digits it contains. for (var i=0; i<=inputValue.length; i++) { if (isANumber(inputValue.charAt(i))) { digitsFound++
}
}
// If inputValue contains at least 10
// digits, assume it is a valid phone number.
if (digitsFound >= 10) { return true
}
else {
return false
}
}
//Calling the isAValidPhoneNumber() function if (iisAValidPhoneNumber(inputValue) {
alert(”We can't contact you via phone unless you give us your phone number
(make sure to include your area code). Thanks!”)
}
228 Part IV: Interacting with Users
The code you see in Listing 12-6 checks to see that a value contains at least ten digits; if so, that value passes the test as a valid telephone number.
Sometimes you want to create more rigid patterns than this. For example, you may want to ensure that users include parentheses and dashes in their telephone numbers. For an example of how to accomplish this (and some caveats), see the sidebar “An alternative approach to pattern-matching.”
Form-leVel Validation
Sometimes you want to validate fields immediately, as soon as a user enters a value or tabs away from the field. (Listing 12-4 shows you an example of independent field validation.)
But sometimes you want to wait until the user finishes entering information before you begin your validation. For example, the Webmeister form allows users to specify whether they want to be contacted by e-mail or by telephone. At least one option must be selected, but triggering validation the instant a user tabs away from the e-mail field would be useless (and annoying). After all, that user might very well be intending to select the phone number option; you have no way of knowing until the user finishes filling out the entire form.
An alternative approach to pattern-matching
If you need to define a more rigid pattern than the telephone number example that I describe in Listing 12-6, take a look at the JavaScript code in this sidebar, which requires that users enter a phone number in the following format:
(512)555-1212
As you see in the following example, the sub-string() method associated with the built-in JavaScript string object lets you break a value into chunks and ensure that each chunk is valid. For example, this code instructs the interpreter to extract and inspect the parentheses, area code, exchange, and line portions of the phone number separately.
The benefit of this approach? It ensures that users type exactly what you want them to type, which reduces the chance of miscommunication. The drawback is that you're expecting a user to type a bunch of characters exactly the way you want — a process that is difficult at best! (Keep in mind that the Web is global, and patterns that might be familiar to you might not be familiar at all to folks in other parts of the world.)
A good design rule to follow is this: If you absolutely must gather information in a specific format, by all means adapt this example of JavaScript code for your own purposes. But if you can get by with fewer checks (like the phone number validation routine that I describe in Listing 12-6), go for it.
Chapter 12: Handling Forms 229
function isAPhoneNumber(entry){ if (entry) {
// Set openParen = to the first character of entry. var openParen = entry.substring(0,1)
// Set areaCode = to the next 3 characters. var areaCode = entry.substring(1,4)
// Set closeParen = to the 5th character. var closeParen = entry.substring(4,5)
// Set exchange = to characters 6, 7, and 8. var exchange = entry.substring(5,8)
// Set dash = to the 9th character. var dash = entry.substring(8,9)
// Set line = to the 10th through 13th characters. var line = entry.substring(9,13)
Previous << 1 .. 72 73 74 75 76 77 < 78 > 79 80 81 82 83 84 .. 118 >> Next