Subversion-Projekte lars-tiefland.zeldi.de_alt

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
2 lars 1
/*
2
 * Código de identificación fiscal ( CIF ) is the tax identification code for Spanish legal entities
3
 * Further rules can be found in Spanish on http://es.wikipedia.org/wiki/C%C3%B3digo_de_identificaci%C3%B3n_fiscal
4
 *
5
 * Spanish CIF structure:
6
 *
7
 * [ T ][ P ][ P ][ N ][ N ][ N ][ N ][ N ][ C ]
8
 *
9
 * Where:
10
 *
11
 * T: 1 character. Kind of Organization Letter: [ABCDEFGHJKLMNPQRSUVW]
12
 * P: 2 characters. Province.
13
 * N: 5 characters. Secuencial Number within the province.
14
 * C: 1 character. Control Digit: [0-9A-J].
15
 *
16
 * [ T ]: Kind of Organizations. Possible values:
17
 *
18
 *   A. Corporations
19
 *   B. LLCs
20
 *   C. General partnerships
21
 *   D. Companies limited partnerships
22
 *   E. Communities of goods
23
 *   F. Cooperative Societies
24
 *   G. Associations
25
 *   H. Communities of homeowners in horizontal property regime
26
 *   J. Civil Societies
27
 *   K. Old format
28
 *   L. Old format
29
 *   M. Old format
30
 *   N. Nonresident entities
31
 *   P. Local authorities
32
 *   Q. Autonomous bodies, state or not, and the like, and congregations and religious institutions
33
 *   R. Congregations and religious institutions (since 2008 ORDER EHA/451/2008)
34
 *   S. Organs of State Administration and regions
35
 *   V. Agrarian Transformation
36
 *   W. Permanent establishments of non-resident in Spain
37
 *
38
 * [ C ]: Control Digit. It can be a number or a letter depending on T value:
39
 * [ T ]  -->  [ C ]
40
 * ------    ----------
41
 *   A         Number
42
 *   B         Number
43
 *   E         Number
44
 *   H         Number
45
 *   K         Letter
46
 *   P         Letter
47
 *   Q         Letter
48
 *   S         Letter
49
 *
50
 */
51
$.validator.addMethod( "cifES", function( value, element ) {
52
	"use strict";
53
 
54
	if ( this.optional( element ) ) {
55
		return true;
56
	}
57
 
58
	var cifRegEx = new RegExp( /^([ABCDEFGHJKLMNPQRSUVW])(\d{7})([0-9A-J])$/gi );
59
	var letter  = value.substring( 0, 1 ), // [ T ]
60
		number  = value.substring( 1, 8 ), // [ P ][ P ][ N ][ N ][ N ][ N ][ N ]
61
		control = value.substring( 8, 9 ), // [ C ]
62
		all_sum = 0,
63
		even_sum = 0,
64
		odd_sum = 0,
65
		i, n,
66
		control_digit,
67
		control_letter;
68
 
69
	function isOdd( n ) {
70
		return n % 2 === 0;
71
	}
72
 
73
	// Quick format test
74
	if ( value.length !== 9 || !cifRegEx.test( value ) ) {
75
		return false;
76
	}
77
 
78
	for ( i = 0; i < number.length; i++ ) {
79
		n = parseInt( number[ i ], 10 );
80
 
81
		// Odd positions
82
		if ( isOdd( i ) ) {
83
 
84
			// Odd positions are multiplied first.
85
			n *= 2;
86
 
87
			// If the multiplication is bigger than 10 we need to adjust
88
			odd_sum += n < 10 ? n : n - 9;
89
 
90
		// Even positions
91
		// Just sum them
92
		} else {
93
			even_sum += n;
94
		}
95
	}
96
 
97
	all_sum = even_sum + odd_sum;
98
	control_digit = ( 10 - ( all_sum ).toString().substr( -1 ) ).toString();
99
	control_digit = parseInt( control_digit, 10 ) > 9 ? "0" : control_digit;
100
	control_letter = "JABCDEFGHI".substr( control_digit, 1 ).toString();
101
 
102
	// Control must be a digit
103
	if ( letter.match( /[ABEH]/ ) ) {
104
		return control === control_digit;
105
 
106
	// Control must be a letter
107
	} else if ( letter.match( /[KPQS]/ ) ) {
108
		return control === control_letter;
109
	}
110
 
111
	// Can be either
112
	return control === control_digit || control === control_letter;
113
 
114
}, "Please specify a valid CIF number." );