HTML and CSS Reference
In-Depth Information
}
As you can see, the
isEmpty
method accepts a single parameter, which is the
value to be validated. By default, it will return false unless the rest of the
implementation returns true.
Use the following validation code to complete the validator.
...
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){
if(value == '' || value == null || value === false){
return true;
}
return false;
}
}
From the preceding implementation, you can see that the conditional statement
returns true if the value is an empty string, null, or false.
NOTE:
As you can see, I have used
===
to compare the value with
false. A
==
comparison loosely compares values. For example,
0 ==
false
will return true. 0 is not necessarily an empty value, so using
0
=== false
will return false, as 0 and false are two completely different
types.
isTypeOf
The next validator will check to see whether a value is a type of object. This is a
relatively simple method. It uses
instanceof
to see whether an object is an
instance of another object. The method accepts a value and a type. However,
the type must not be a string but the original object, as shown in the following
code snippet.