jsbeans

jsbeans  1.0.0

jsbeans > jsbeans > Validator.fiscalCode.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 fiscal code.
 * @for Validator
 * @namespace jsbeans
 * @static
 * */
jsbeans.Validator.fiscalCode = {
	/**
	 * Default messages for Validator.fiscalCode extension
	 * @property fiscalCode.messages
	 * @type JSON
	 * @static
	 * */
	messages: {
		en: "is not a valid fiscal code",
		it: "non e' un codice fiscale valido"
	}
	/**
	 * Checks if {@param} <code class="param">obj</code>'s value is a valid <a href="http://it.wikipedia.org/wiki/Codice_fiscale">fiscal code</a>.<br/>
	 * If {@param} <code class="param">forceStrict</code> is set to true the validation id not only formal (digits and characters in the right place).<br/>
	 * A valid format is ('D' for digits, 'C' for character): CCCCCCDDCDDCDDDC
	 * @method assertFiscalCode
	 * @param obj {DOM} input to check
	 * @param [forceStrict] {boolean} true to check fiscal code integrity, not only formally. Default false.
	 * @return {boolean} true if <code class="param">obj</code>'s value is a valid fiscal code.
	 * @static
	 * @author Francesco Mele
	 * @credits Paolo Biavati for strict mode implementation
	 * */
	,assertFiscalCode: function(/*DOM*/obj) {
		var forceStrict = arguments.length == 2 && arguments[1];
		var val = (obj.value || "").replace(/^\s+|\s+$/g, "");
		if (forceStrict) {
			var isValid = function(val) {
			   var cf = val.toUpperCase();
			   var cfReg = /^[A-Za-z]{6}[0-9LMNPQRSTUVlmnpqrstuv]{2}[ABCDEHLMPRSTabcdehlmprst]{1}[0-9LMNPQRSTUVlmnpqrstuv]{2}[A-Za-z]{1}[0-9LMNPQRSTUVlmnpqrstuv]{3}[A-Za-z]{1}$/;
			   if (!cfReg.test(cf)) {
				   return false;
			   }
			   var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			   var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
			   var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			   var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
			   var s = 0;
			   for (var i = 1; i <= 13; i += 2) {
				   s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
			   }
			   for (var i = 0; i <= 14; i += 2) {
				   s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
			   }
			   if (s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0)) {
				   return false;
			   }
			   return true;
			};
			return val == "" || isValid(val);
		}
		else {
			return val == "" || /^[A-Za-z]{6}[0-9LMNPQRSTUVlmnpqrstuv]{2}[ABCDEHLMPRSTabcdehlmprst]{1}[0-9LMNPQRSTUVlmnpqrstuv]{2}[A-Za-z]{1}[0-9LMNPQRSTUVlmnpqrstuv]{3}[A-Za-z]{1}$/.test(val);
		}
	}
};

Copyright © 2016 Francesco Mele. All rights reserved.