HTML and CSS Reference
In-Depth Information
This eases the need to perform vast amounts of validation to check to see
whether an object passed to a method is of a certain type, as upon execution, if
it isn't, the method will automatically throw an exception or error.
To get around this, you need to validate parameters passed to your accessors.
The most common type of validation will be to validate against type. You can
check to see whether a value is an object, array, number, or string.
You will want to use some of these validators more than once, so it's worth
creating an object to store all of the validation methods. To do this, create a new
JavaScript file called validator.js within the js/app/utility folder (if the folder
doesn't exist, create it).
You will create a constructorless object. This will allow you to use the object's
methods without having to instantiate it. Use the following code snippet to start
creating your validation object.
var app = app || {};
app.utility = app.utility || {};
/**
* Validator object has static methods
* to check to easily validate values
*/
app.utility.validator = {}
With the validator utility in place, we can begin to create the validation methods.
isEmpty
The first easy validation method will be to check to see whether a value is
empty. Create a new method within the validator called isEmpty , as shown in the
following code example.
...
app.utility.validator = {
/**
* Checks to see whether a value is empty or not
* Returns true if it is, or false if it isn't
* @param {String|Object} value
* @return {Bool}
*/
isEmpty: function(value){
return false;
}
 
Search WWH ::




Custom Search