Skip to content

Added CNH Validation (Brazilian License Driver) #2221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/additional/cnhBR.js
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;

firstChar = value.charAt(0)

if(firstChar.repeat(11) == value) {
return false;
}

// Step 1 - using first Check Number:
for ( var 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" );
3 changes: 2 additions & 1 deletion src/localization/messages_pt_BR.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ $.extend( $.validator.messages, {
zipcodeUS: "Por favor, forne&ccedil;a um c&oacute;digo postal americano v&aacute;lido.",
ziprange: "O c&oacute;digo postal deve estar entre 902xx-xxxx e 905xx-xxxx",
cpfBR: "Por favor, forne&ccedil;a um CPF v&aacute;lido.",
nisBR: "Por favor, forne&ccedil;a um NIS/PIS v&aacute;lido"
nisBR: "Por favor, forne&ccedil;a um NIS/PIS v&aacute;lido",
cnhBR: "Por favor, forne&ccedil;a um CNH v&aacute;lido."
} );