1153Chapter 43 .Data-Entry Validation This function uses a (Web site directory)

1153Chapter 43 .Data-Entry Validation This function uses a Boolean OR operator (||) to test for the existence of a null value or an empty string in the value passed to the function. Because the name of the function implies a true response if the entry is empty, that value is the one that returns to the calling statement if either condition is true. Because a returnstatement halts further processing of a function, the return false statement lies outside of the ifconstruction. If processing reaches this statement, the inputStr value has failed the test. If this seems like convoluted logic return truewhen the value is empty you can also define a function that returns the inverse values. You can name it isNotEmpty(). As it turns out, however, typical processing of an empty entry is better served when the test returns a truethan when the value is empty aiding the if construction that calls the function in the first place. isPosInteger() This next function examines each character of the value to make sure that only numbers from 0 through 9 with no punctuation or other symbols exist. The goal of the function in Listing 43-3 is to weed out any value that is not a positive integer. Listing 43-3: Test for Positive Integers // general purpose function to see if a suspected numeric input // is a positive integer function isPosInteger(inputVal) { inputStr = inputVal.toString() for (var i = 0; i < inputStr.length; i++) { var oneChar = inputStr.charAt(i) if (oneChar < 0 || oneChar > 9 ) { return false } } return true } Notice that this function makes no assumption about the data type of the value that is passed as a parameter. If the value had come directly from a text object, it would already be a string and the line that forced data conversion to a string would be unnecessary. But to generalize the function, the conversion is included to accommodate the possibility that it may be called from another statement that has a numeric value to check. The function requires you to convert the input value to a string because it performs a character-by-character analysis of the data. A forloop picks apart the value one character at a time. Rather than force the script to invoke the string.charAt() method twice for each time through the loop (inside the ifcondition), one statement assigns the results of the method to a variable, which is then used twice in the if condition. Placing the results of the charAt()method into a variable makes the ifcondition shorter and easier to read and also is microscopically more efficient.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Leave a Reply