1152 Part V . Putting JavaScript to Work (Web hosting providers)
1152 Part V . Putting JavaScript to Work Building a Library of Filter Functions A number of basic data-validation processes function repeatedly in form-intensive HTML pages. Filters for integers only, numbers only, empty entries, alphabet letters only, and the like are put to use every day. If you maintain a library of generalizable functions for each of your data-validation tasks, you can drop these functions into your scripts at a moment s notice and be assured that they will work. For NN3+ and IE4+, you can also create the library of validation functions as a separate .jslibrary file and link the scripts into any HTML file that needs them. Making validation functions generalizable requires careful choice of wording and logic so that they return Boolean values that make syntactical sense when called from elsewhere in your scripts. As you see later in this chapter, when you build a larger framework around smaller functions, each function is usually called as part of an if…else conditional statement. Therefore, assign a name that fits logically as part of an if clause in plain language. For example, you can name a function that checks whether an entry is empty isEmpty(). The calling statement for this function is: if (isEmpty(value)) { … } From a plain-language perspective, the expectation is that the function returns true if the passed value is empty. With this design, the statements nested in the if construction handle empty entry fields. I revisit this design later in this chapter when I start stacking multiple-function calls together in a larger validation routine. To get you started with your library of validation functions, this chapter provides some building blocks that you can learn from and use as starting points for more specific filters of your own design. Some of these functions are put to use in the JavaScript application in Chapter 50. isEmpty() This first function, shown in Listing 43-2, checks to see if the incoming value is either empty or null. Adding a check for nullmeans that you can use this function for purposes other than just text-object validation. For example, if another function defines three parameter variables, but the calling function passes only two, the third variable is set to null. If the script then performs a data-validation check on all parameters, the isEmpty() function responds that the null value is devoid of data. Listing 43-2: Is an Entry Empty or Null? // general purpose function to see if an input value has been // entered at all function isEmpty(inputStr) { if (inputStr == null || inputStr == ) { return true } return false }
In case you need quality webspace to host and run your web applications, try our personal web hosting services.