1156 Part V . Putting JavaScript to Work (Disney web site)
1156 Part V . Putting JavaScript to Work Listing 43-6: A Custom Validation Function // function to determine if value is in acceptable range // for this application function inRange(inputStr) { num = parseInt(inputStr) if (num < 1 || num > 586 && num < 596 || num > 599 && num < 700 || num > 728) { return false } return true } For this application, you need to see if an entry falls within multiple ranges of acceptable numbers. The first statement of the inRange()function converts the incoming value to a number (via the parseInt()function) so that the value can be compared numerically against maximum and minimum values of several ranges within the database. Following the logic of the previous validation functions, the if condition looks for values outside the acceptable range, so it can alert the user and return a false value. The if condition is quite a long sequence of operators. As you noticed in the list of operator precedence (Chapter 40), the Boolean AND operator (&&) has prece dence over the Boolean OR operator (||). Therefore, the AND expressions evaluate first, followed by the OR expressions. Parentheses may help you better visualize what s going on in that monster condition: if (num < 1 || (num > 586 && num < 596) ||(num > 599 && num < 700) || num > 728) In other words, you exclude four possible ranges from consideration: . Values less than 1 . Values between 586 and 596 . Values between 599 and 700 . Values greater than 728 Any value for which any one of these tests is true yields a Boolean false from this function. Combining all these tests into a single condition statement eliminates the need to construct an otherwise complex series of nested if constructions. Combining Validation Functions When you design a page that requests a particular kind of text input from a user, you often need to call more than one data-validation function to handle the entire job. For example, if you merely want to test for a positive integer entry, your valida tion should test for the presence of any entry as well as the validation as an integer. After you know the kind of permissible data that your script will use after valida tion, you re ready to plot the sequence of data validation. Because each page s vali dation task is different, I supply some guidelines to follow in this planning rather than prescribe a fixed route for all to take.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.