Apache web server tutorial - 1155Chapter 43 .Data-Entry Validation isNumber() The final numeric

1155Chapter 43 .Data-Entry Validation isNumber() The final numeric filter function in this series enables any integer or floating- point number to pass while filtering out all others (Listing 43-5). All that distinguishes an integer from a floating-point number for data-validation purposes is the decimal point. Listing 43-5: Testing for a Decimal Point // general purpose function to see if a suspected numeric input // is a positive or negative number function isNumber(inputVal) { oneDecimal = false inputStr = inputVal.toString() for (var i = 0; i < inputStr.length; i++) { var oneChar = inputStr.charAt(i) if (i == 0 && oneChar == - ) { continue } if (oneChar == . && !oneDecimal) { oneDecimal = true continue } if (oneChar < 0 || oneChar > 9 ) { return false } } return true } Anticipating the worst, however, the function cannot simply treat a decimal point at any position within the string as a valid character. Such an act assumes that no one would ever enter more than one decimal point into a numeric text field. Only one decimal point is allowed for this function (as well as for JavaScript math). Therefore, you add a Boolean flag variable (oneDecimal) to the function and a separate if condition that sets that flag to true when the function encounters the first decimal point. Should another decimal point appear in the string, the final if statement gets a crack at the character. Because the character falls outside the ASCII range of 0 through 9, it fails the entire function. If you want to accept only positive floating-point numbers, you can make a new version of this function by removing the statement that lets the leading minus sign through. Be aware that this function works only for values that are not represented in exponential notation. Custom validation functions The listings shown so far in this chapter should give you plenty of source material to use in writing customized validation functions for your applications. Listing 43-6 shows an example of such an application-specific variation (extracted from the application in Chapter 50).
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Leave a Reply