-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional: Add Brazillian CNH number (Carteira Nacional de Habilitac…
…ao) (#2234)
- Loading branch information
1 parent
e83a240
commit fcf782e
Showing
4 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Brazillian CNH number (Carteira Nacional de Habilitacao) is the License Driver number. | ||
* CNH numbers have 11 digits in total: 9 numbers followed by 2 check numbers that are being used for validation. | ||
*/ | ||
$.validator.addMethod( "cnhBR", function( value ) { | ||
|
||
// Removing special characters from value | ||
value = value.replace( /([~!@#$%^&*()_+=`{}\[\]\-|\\:;'<>,.\/? ])+/g, "" ); | ||
|
||
// Checking value to have 11 digits only | ||
if ( value.length !== 11 ) { | ||
return false; | ||
} | ||
|
||
var sum = 0, dsc = 0, firstChar, | ||
firstCN, secondCN, i, j, v; | ||
|
||
firstChar = value.charAt( 0 ); | ||
|
||
if ( new Array( 12 ).join( firstChar ) === value ) { | ||
return false; | ||
} | ||
|
||
// Step 1 - using first Check Number: | ||
for ( i = 0, j = 9, v = 0; i < 9; ++i, --j ) { | ||
sum += +( value.charAt( i ) * j ); | ||
} | ||
|
||
firstCN = sum % 11; | ||
if ( firstCN >= 10 ) { | ||
firstCN = 0; | ||
dsc = 2; | ||
} | ||
|
||
sum = 0; | ||
for ( i = 0, j = 1, v = 0; i < 9; ++i, ++j ) { | ||
sum += +( value.charAt( i ) * j ); | ||
} | ||
|
||
secondCN = sum % 11; | ||
if ( secondCN >= 10 ) { | ||
secondCN = 0; | ||
} else { | ||
secondCN = secondCN - dsc; | ||
} | ||
|
||
return ( String( firstCN ).concat( secondCN ) === value.substr( -2 ) ); | ||
|
||
}, "Please specify a valid CNH number" ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
QUnit.test( "cnhBR", function( assert ) { | ||
var method = methodTest( "cnhBR" ); | ||
assert.ok( method( "00000000119" ), "Valid driver's license number" ); | ||
assert.ok( !method( "11111111111" ), "Invalid driver's license number" ); | ||
assert.ok( !method( "asdf" ), "Invalid driver's license number" ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters