Subversion-Projekte lars-tiefland.zeldi.de_alt

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
2 lars 1
/*
2
 * Brazillian CPF number (Cadastrado de Pessoas Físicas) is the equivalent of a Brazilian tax registration number.
3
 * CPF numbers have 11 digits in total: 9 numbers followed by 2 check numbers that are being used for validation.
4
 */
5
$.validator.addMethod( "cpfBR", function( value, element ) {
6
	"use strict";
7
 
8
	if ( this.optional( element ) ) {
9
		return true;
10
	}
11
 
12
	// Removing special characters from value
13
	value = value.replace( /([~!@#$%^&*()_+=`{}\[\]\-|\\:;'<>,.\/? ])+/g, "" );
14
 
15
	// Checking value to have 11 digits only
16
	if ( value.length !== 11 ) {
17
		return false;
18
	}
19
 
20
	var sum = 0,
21
		firstCN, secondCN, checkResult, i;
22
 
23
	firstCN = parseInt( value.substring( 9, 10 ), 10 );
24
	secondCN = parseInt( value.substring( 10, 11 ), 10 );
25
 
26
	checkResult = function( sum, cn ) {
27
		var result = ( sum * 10 ) % 11;
28
		if ( ( result === 10 ) || ( result === 11 ) ) {
29
			result = 0;
30
		}
31
		return ( result === cn );
32
	};
33
 
34
	// Checking for dump data
35
	if ( value === "" ||
36
		value === "00000000000" ||
37
		value === "11111111111" ||
38
		value === "22222222222" ||
39
		value === "33333333333" ||
40
		value === "44444444444" ||
41
		value === "55555555555" ||
42
		value === "66666666666" ||
43
		value === "77777777777" ||
44
		value === "88888888888" ||
45
		value === "99999999999"
46
	) {
47
		return false;
48
	}
49
 
50
	// Step 1 - using first Check Number:
51
	for ( i = 1; i <= 9; i++ ) {
52
		sum = sum + parseInt( value.substring( i - 1, i ), 10 ) * ( 11 - i );
53
	}
54
 
55
	// If first Check Number (CN) is valid, move to Step 2 - using second Check Number:
56
	if ( checkResult( sum, firstCN ) ) {
57
		sum = 0;
58
		for ( i = 1; i <= 10; i++ ) {
59
			sum = sum + parseInt( value.substring( i - 1, i ), 10 ) * ( 12 - i );
60
		}
61
		return checkResult( sum, secondCN );
62
	}
63
	return false;
64
 
65
}, "Please specify a valid CPF number" );