diff --git a/src/additional/cnhBR.js b/src/additional/cnhBR.js new file mode 100644 index 000000000..67ea7965a --- /dev/null +++ b/src/additional/cnhBR.js @@ -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" ); diff --git a/src/localization/messages_pt_BR.js b/src/localization/messages_pt_BR.js index 08c2c94bd..4f9effad8 100644 --- a/src/localization/messages_pt_BR.js +++ b/src/localization/messages_pt_BR.js @@ -73,5 +73,6 @@ $.extend( $.validator.messages, { zipcodeUS: "Por favor, forneça um código postal americano válido.", ziprange: "O código postal deve estar entre 902xx-xxxx e 905xx-xxxx", cpfBR: "Por favor, forneça um CPF válido.", - nisBR: "Por favor, forneça um NIS/PIS válido" + nisBR: "Por favor, forneça um NIS/PIS válido", + cnhBR: "Por favor, forneça um CNH válido." } ); diff --git a/test/additional/cnhBR.js b/test/additional/cnhBR.js new file mode 100644 index 000000000..99bdf847d --- /dev/null +++ b/test/additional/cnhBR.js @@ -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" ); +} ); diff --git a/test/index.html b/test/index.html index 84ecc38fd..bf22a640a 100644 --- a/test/index.html +++ b/test/index.html @@ -15,6 +15,7 @@ + @@ -447,6 +448,12 @@
+ + +