jsbeans

jsbeans  1.0.0

jsbeans > jsbeans > Validator.length.js (source view)
Search:
 
Filters
/*!
 * 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 exact input's length
 * @for Validator
 * @namespace jsbeans
 * @static
 * */
jsbeans.Validator.length = {
	/**
	 * Default messages for Validator.length extension
	 * @property length.messages
	 * @type JSON
	 * @static
	 * */
	messages: {
		en: "is not as long as aspected",
		it: "non e' della lunghezza attesa"
	},
	/**
	 * Checks if {@param} <code class="param">input</code>'s length is as aspected.
	 * @method assertLength
	 * @param input {DOM} the input to validate
	 * @param lengths {Integer | Array[&lt;Integer>]} may be just an Integer or an Array of Integers (<em>[3,7,11]</em>) if input's value may have more then one length (think about European CIN that can have 8 OR 11 characters)
	 * @return {boolean} true if <code class="param">input</code>'s length is as long as one of the passed length.
	 * @static
	 * */
	assertLength: function(obj, lengths) {
		var val = (obj.value || "").replace(/^\s+|\s+$/g, "");
		if (val == "") {
			return true;
		}
		var len = val.length;
		
		var lenInt = parseInt(lengths, 10);
		if (isNaN(lenInt)) {
			var lengths = (eval("({t:" + lengths + "})")).t;
			for (var i = 0; i < lengths.length; i++) {
				if (parseInt(lengths[i], 10) == len) {
					return true;
				}
			}
			return false;
		}
		else {
			return lenInt == len;
		}
		
	}
};

Copyright © 2016 Francesco Mele. All rights reserved.