Javascript for dummies 4th edition - Veer E.V
ISBN: 0-7645-7659-3
Download (direct link):


}
// Otherwise, we still have to check the rest of
// the digits, so step through the inputValue one
// character at a time and set result = false
// if any non-numeric digits are encountered.
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
}
////////////////////////////////////////////////////// // Checks to see whether an input value contains β@β
// and β.β
////////////////////////////////////////////////////// function isAValidEmail(inputValue) {
var foundAt = false var foundDot = false
// Step through the inputValue looking for // β@β and β.β
for (var i=0; i<=inputValue.length; i++) { if (inputValue.charAt(i) == β@β ) { foundAt = true
}
else if (inputValue.charAt(i) == β.β) { foundDot = true
}
Chapter 12: Handling Forms 233
}
// If both β@β and β.β were found, assume // the e-mail address is valid; otherwise,
// return false so the calling code knows // the e-mail address is invalid.
if (foundAt && foundDot) { return true
}
else {
return false
}
}
////////////////////////////////////////////////////// // Checks to see if an input value contains ten or more // numbers. This approach lets users type in U.S.-// style phone formats, such as (123)456-7890, as // well as European-style (such as 123.456.7890). ////////////////////////////////////////////////////// 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
}
}
////////////////////////////////////////////////////// // Check for the existence of characters.
// (Spaces aren't counted.) //////////////////////////////////////////////////////
(continued)
234 Part IV: Interacting with Users______________
Listing 12-7 (continued)
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
}
//////////////////////////////////////////////////////
// Perform cross-field checks that can't be performed // until all of the data has been entered. //////////////////////////////////////////////////////
// validateForm() performs all dependent field validation
function validateForm() {
var rc = true
// Dependent check #1: ensuring a service category is selected
///////////////////////////////////////////////////
// Visitors need to check one of the following // choices in order to receive an accurate quote:
// whether they're interested
// in design, maintenance, or promotion services.
///////////////////////////////////////////////////
if (!document.quoteForm.designChoice.checked && !document.quoteForm.maintChoice.checked && !document.quoteForm.promoChoice.checked) {
alert(βPlease check whether you're interested in our design, maintenance, or promotion services so we can give you a more accurate quote. Thanks!β) rc = false
}
// Dependent check #2: ensuring that a company name exists if a // user checked "employeeβ
Chapter 12: Handling Forms 235
///////////////////////////////////////////////////
// If visitors are employees, they need to specify // the name of their company. ///////////////////////////////////////////////////
if (document.quoteForm.bizChoice[1].checked) { if (Idocument.quoteForm.corpName.value) {
alert("You've specified that you're an employee, so could you please type in the name of the company you work for? Thanks!β) rc = false
}
}
// Dependent check #3: double-checking that both first and // last names exist
///////////////////////////////////////////////////
// Visitors need to include their first and last // names.
///////////////////////////////////////////////////
if (Idocument.quoteForm.firstName.value || Idocument.quoteForm.lastName.value) {
alert("Please type in your entire name (both first and last). Thanks!β) rc = false
}
// Dependent check #4: ensuring that users enter either an e-mail // address or a phone number
///////////////////////////////////////////////////
// Visitors need to specify either an e-mail // address or a telephone number. /////////////////////////////////////////////////// if (Idocument.quoteForm.emailChoice.checked && Idocument.quoteForm.phoneChoice.checked) {
alert("Please let us know whether you'd like us to contact you by e-mail or by phone. ThanksIβ) rc = false
}
// Dependent check #5: ensuring that an e-mail address exists // (if a user chose the e-mail contact option)

