| 2 |
lars |
1 |
/*
|
|
|
2 |
* Brazillian CNH number (Carteira Nacional de Habilitacao) is the License Driver number.
|
|
|
3 |
* CNH numbers have 11 digits in total: 9 numbers followed by 2 check numbers that are being used for validation.
|
|
|
4 |
*/
|
|
|
5 |
$.validator.addMethod( "cnhBR", function( value ) {
|
|
|
6 |
|
|
|
7 |
// Removing special characters from value
|
|
|
8 |
value = value.replace( /([~!@#$%^&*()_+=`{}\[\]\-|\\:;'<>,.\/? ])+/g, "" );
|
|
|
9 |
|
|
|
10 |
// Checking value to have 11 digits only
|
|
|
11 |
if ( value.length !== 11 ) {
|
|
|
12 |
return false;
|
|
|
13 |
}
|
|
|
14 |
|
|
|
15 |
var sum = 0, dsc = 0, firstChar,
|
|
|
16 |
firstCN, secondCN, i, j, v;
|
|
|
17 |
|
|
|
18 |
firstChar = value.charAt( 0 );
|
|
|
19 |
|
|
|
20 |
if ( new Array( 12 ).join( firstChar ) === value ) {
|
|
|
21 |
return false;
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
// Step 1 - using first Check Number:
|
|
|
25 |
for ( i = 0, j = 9, v = 0; i < 9; ++i, --j ) {
|
|
|
26 |
sum += +( value.charAt( i ) * j );
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
firstCN = sum % 11;
|
|
|
30 |
if ( firstCN >= 10 ) {
|
|
|
31 |
firstCN = 0;
|
|
|
32 |
dsc = 2;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
sum = 0;
|
|
|
36 |
for ( i = 0, j = 1, v = 0; i < 9; ++i, ++j ) {
|
|
|
37 |
sum += +( value.charAt( i ) * j );
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
secondCN = sum % 11;
|
|
|
41 |
if ( secondCN >= 10 ) {
|
|
|
42 |
secondCN = 0;
|
|
|
43 |
} else {
|
|
|
44 |
secondCN = secondCN - dsc;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
return ( String( firstCN ).concat( secondCN ) === value.substr( -2 ) );
|
|
|
48 |
|
|
|
49 |
}, "Please specify a valid CNH number" );
|