/*!
* 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 counting characters in inputs.
* @for Validator
* @namespace jsbeans
* @static
* */
jsbeans.Validator.charCount = {
/**
* Default messages for Validator.charCount extension
* @property charCount.messages
* @type JSON
* @static
* */
messages: {
en: "exceedes the maximum number of characters allowed for the field",
it: "supera il numero di caretteri massimo consentito per il campo"
},
/**
* Core counter. It takes same arguments (and it is used by) as {@method} <code class="methd">assertCharCount</code>.<br/>
* It may be useful to display current number of characters. Beware of firing with <code>onkeyup</code> event.
* @method charCount.getCount
* @param input {DOM} input to check
* @param params {Integer | JSON} limit for number of chars OR a JSON with <code>limit</code> (Integer, required), <code>skip</code> (Integer, optional) and <code>excludes</code> (Array, optional).
* @return {Integer} the current number of characters in <code class="param">input</code>; default <code>0</code>
* @static
*/
getCount: function(input, params) {
if (input == null || input.value == null || input.value == "") {
return 0;
}
if (!params._parsed) {
ps = jsbeans.Validator.charCount._parseParams(params);
}
else {
ps = params;
}
var v = input.value;
var exs = ps.excludes;
var res = 0;
out: for (var i = 0, c; c = v.charAt(i); i++) {
for (var j = 0; j < exs.length; j++) {
if (exs[j] == c) {
continue out;
}
}
res++;
}
return res;
},
/**
* List of characters excluded from counting. By defalut it has just the newline char, the most common char for textareas (according to developers).<br/>
* It may be modified according to yours needs (just remember it's static) or use the optional <code>excludes</code> parameter invoking the validation method.
* @property charCount.EXCLUDES
* @type Array
* @static
*/
EXCLUDES: [
'\n'
],
/**
* Checks if {@param} <code class="param">input</code>'s value contains not more than a given number of characters.<br/>
* This method does NOT take into account some characters as defined by <code>jsbeans.Validator.charCount.EXLUDES</code> array.<br/>
* {@param} <code class="param">params</code> may be an just Integer to check the maximum mumber of characters or a JSON in the following form:
* <ul>
* <li><code>limit</code> <Integer> (<strong>required</strong>): maximum number of characters aspected</li>
* <li><code>excludes</code> <Array>: if provided this array overrides <code>jsbeans.Validator.charCount.EXCLUDES</code></li>
* </ul>
* {@param} <code class="param">excludes</code> items are checked using the <code>==</code> (equals) operator.
* <br/>Samples:
* <ol>
* <li><code>["charCount=10"]</code>: returns false if {@param} <code class="param">input</code>'s value contains 11 or more characters</li>
* <li><code>["charCount={limit:10,excludes:[]}"]</code>: returns false if {@param} <code class="param">input</code>'s value contains 11 or more characters, none excluded.</li>
* </ol>
* @method assertCharCount
* @param input {DOM} input to check
* @param params {Integer | JSON} limit for number of characters OR a JSON with <code>limit</code> (Integer, required) and <code>excludes</code> (Array, optional).
* @return {boolean} true if <code class="param">input</code>'s value has less than specified rules (see description).
* @static
* */
assertCharCount: function(input, params) {
var ps = {};
try {
if (!params._parsed) {
ps = jsbeans.Validator.charCount._parseParams(params);
}
}
catch(err) {
return false;
}
return jsbeans.Validator.charCount.getCount(input, ps) <= ps.limit;
},
/**
* Utility method. It parses parameters for <code class="methd">getCount</code> and <code class="methd">assertCharCount</code> methods.<br/>
* Method intended to be private.
* @method charCount._parseParams
* @param params {Integer | JSON} limit for number of characters OR a JSON with <code>limit</code> (Integer, required), <code>skip</code> (Integer, optional) and <code>excludes</code> (Array, optional).
* @return {JSON} a JSON with parsed parameters as passed and/or with default values. It throws <code>Error</code>s as validation issue.
* @private
* @static
*/
_parseParams: function(params) {
// defaults
var ps = {
limit: -1,
excludes: jsbeans.Validator.charCount.EXCLUDES,
// prevent double parsing
_parsed: false
};
// checks for params
var test = parseInt(params);
if (isNaN(test)) {
// presuming a JSON has been passed
if (params.limit) {
test = params;
}
else {
test = eval("(" + params + ")");
}
// if params is a JSON it MUST have the 'limit' property
if (!test.limit) {
throw new Error('limit is mandatory');
}
else {
var n = parseInt(test.limit);
// 'limit' MUST be an Integer
if (isNaN(n)) {
throw new Error('limit is non an Integer');
}
ps.limit = test.limit;
}
if (!!test.excludes) {
// see jsbeans.Array
var ex = (typeof(test.excludes) === "object" && (test.excludes).constructor == Array);
// 'excludes' MUST be an Array
if (ex == false) {
throw new Error('excludes is not an Array');
}
else {
// overrride default jsbeans.Validator.charCount.EXCLUDES
ps.excludes = test.excludes;
}
}
}
// just passed an Integer
else {
ps.limit = test;
}
// if here params have been parsed
ps._parsed = true;
return ps;
}
};