/*!
* Copyright (c) 2009 Francesco Mele jsbeans@francescomele.com
*
* This Software is licenced under the LGPL Licence (GNU Lesser General
* Public License).
* In addition to the LGPL Licence the Software is subject to the
* following conditions:
*
* i every modification must be public and comunicated to the Author
* ii every "jsbean" added to this library must be self consistent
* except for the dependence from jsbeans-x.x.x.js
* iii copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Validator extensions for file extensions.<br/>
* Sample usage: file_input: ["file=pdf,txt"]
* @for Validator
* @namespace jsbeans
* @static
* */
jsbeans.Validator.file = {
/**
* Default messages for Validator.file extension
* @property file.messages
* @type JSON
* @static
* */
messages: {
en: "is not an allowed file type",
it: "non e' un tipo file ammesso"
},
/**
* Checks if {@param} <code class="param">input</code>'s value contains a string representing a file matching one of given {@param} <code class="param">extension_list</code>
* @method assertFile
* @param input {DOM} the input to validate
* @param extension_list {String} a CSV (Comma Separeted Values) of allowed extensions. Spaces are permitted (e.g. <code>["file=txt, pdf"]</code>)
* @return {boolean} true if <code class="param">input</code>'s value matches one of given <code class="param">extension_list</code>, case insensitive
* @static
* */
assertFile: function(obj, arg) {
var val = obj.value || "";
if (val == "") {
return true;
}
var sp = val.split(".");
if (sp.length < 2) {
return false;
}
var ext = sp[sp.length - 1].toUpperCase();
var ftypes = (arg || "").split(",");
for (var i = 0, ftype; ftype = ftypes[i]; i++) {
// trimming each value
if (ftype.replace(/^\s\s*/, '').replace(/\s\s*$/, '').toUpperCase() == ext) {
return true;
}
}
return false;
}
};