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

^ Validate numeric fields: The generic isANumber() function assists in validating phone numbers on a form level.
Figure 12-2 shows you what the completed quote request example looks like.
To see the code responsible for Figure 12-2, l ist1207.htm, in its entirety, open the file from the companion CD-ROM.
B] Order form example (from JavaScript For Dummies. 4th edition) - Microsoft Internet Explorer
File Edit View Favoiites Tools Help V
I QP Back * © ' H 3 Ci ^Dsaarch ^Favorites I lu ill u ^2) < ' „ Ld * ^ 21
; Address Çjc:\em\wKe\)sfd4e\scripts\listl206.htm i 0»
Which of our services are you interested in? (Check all that apply.) -
Website design □ Website maintenance G Online promotion D
Why do you want a Website? (Or. if you already have one. what do you use it for?)

Do you want to incorporate photos into your site? yes ® no O
Do you have one or mote products you'd like to promote sell on your site? yes ® no O
Ate you a small business owner, or do you work for a large corporation? small business owner ® employee O
f you work for a corporation, what's the name?
s there anvihirm else vou think we need to know in order to «live vou an accurate mice «note? -
■£] Done i My Computer
E" TextPad - [C:\em... \ fi] 506331 Chl2_f.d... KÏTW 0 The Bat' 5:28 PM
224 Part IV: Interacting with Users_____________
Testing for existence
CO
You can require that users provide a value for an HTML form field by attaching an existence-validation script to one of that field’s event handlers.
In this example, the Webmeister developers want to ensure that folks requesting a quote enter both their first and last names. Listing 12-4 shows you the JavaScript code necessary to implement this common design requirement.
Listing 12-4: Testing for the Existence of an Input Value
function exists(inputValue) { var aCharExists = false
// Step through the inputValue, using the charAt()
// method to detect non-space characters.
for (var i=0; i<=inputValue.length; i++) { if (inputValue.charAt(i) != ” ” && inputValue.charAt(i) != '"') { aCharExists = true break
}
}
return aCharExists
}
//The value of the firstName field is sent to the exists() function as soon as the user tabs away.
<INPUT TYPE=”TEXT” NAME=”firstName” SIZE=”25” onBlur=”if (!exists(this.value)) { alert('Please enter a first name'); }”>
//The value of the lastName field is sent to the exists() function as soon as the user tabs away.
<INPUT TYPE=”TEXT” NAME=”lastName” SIZE=”35” onBlur=”if (!exists(this.value)) { alert('Please enter a last name') }”>
The code in Listing 12-4 works on these principles: The exists() function accepts an input value (named, appropriately enough, inputValue). As soon as the exists() function receives this value, it checks the value to see whether it contains a non-white-space character. Either the non-white-space character or the default value of false is returned to the calling code.
If you look lower in the listing, you see the two input fields that call the exists() function, including this one:
Chapter 12: Handling Forms 225
<INPUT TYPE=”TEXT” NAME=”firstName” SIZE=”25” onBlur=”if (!exists(this.value)) { alert('Please enter a first name'); }”>
The preceding JavaScript statement defines a value for the firstName field’s onBlur event handler. When a user blurs the firstName field, the value of the firstName field is passed to the exists() function. If exists() returns a value of false (the ! operator is shorthand for “if this thing is false”), a pop-up message appears to remind the user to enter a first name. Now, when the user clicks in the Your First Name field and then tabs away without entering a value, the code causes a reminder message to appear (see Figure 12-3).
Testing for a numeric Value
You can require that users provide a valid number for an HTML form field by attaching a numeric validation script to one of that field’s event handlers. For an example of the JavaScript code required to perform this validation, take a peek at Listing 12-5.
Figure 12-3:
Everybody must have (and enter) a name.
226 Part IV: Interacting with Users
Listing 12-5: Testing to Ensure That a Value Is Numeric
//Defining the isANumber() function function i sANumber(inputValue){
// Assume everything is okay right off the bat. var result = true
// If parseFloat() returns false, a non-numeric // character was detected in the first position.
if (IparseFloat(inputValue)) { result = false
}
// Otherwise, check the // rest of the digits.
else {
for (var i=0; i<inputValue.length; i++) { if (inputValue.charAt(i) I = ” ”) {
if (IparseFloat(inputValue.charAt(i))) { result = false break
}
}
}
}
// Return true (inputValue is a valid number) or // false (it's invalid).
return result
}
function isAValidPhoneNumber(inputValue) {
for (var i=0; i<=inputValue.length; i++) {
//Calling the isANumber() function from inside another custom function if (isANumber(inputValue.charAt(i))) { digitsFound++
Previous << 1 .. 71 72 73 74 75 76 < 77 > 78 79 80 81 82 83 .. 118 >> Next