mirror of
https://github.com/jquery-validation/jquery-validation.git
synced 2026-06-12 15:37:05 +02:00
Additional: Add Brazillian CNH number (Carteira Nacional de Habilitacao) (#2234)
This commit is contained in:
committed by
Brahim Arkni
parent
e83a240946
commit
fcf782e2c7
@@ -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" );
|
||||
@@ -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."
|
||||
} );
|
||||
|
||||
@@ -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" );
|
||||
} );
|
||||
@@ -15,6 +15,7 @@
|
||||
<script src="rules.js"></script>
|
||||
<script src="messages.js"></script>
|
||||
<script src="methods.js"></script>
|
||||
<script src="additional/cnhBR.js"></script>
|
||||
<script src="additional/creditcard.js"></script>
|
||||
<script src="additional/netmask.js"></script>
|
||||
<script src="additional/abaRoutingNumber.js"></script>
|
||||
@@ -447,6 +448,12 @@
|
||||
<input name="year"/>
|
||||
<button name="submitForm27" value="someValue" type="submit"><span>Submit</span></button>
|
||||
</form>
|
||||
|
||||
<form id="cnhFormTest">
|
||||
<input id="cnhnumber" name="cnhnumber" required>
|
||||
<button name="submitFormCnh" value="submitFormCnh" type="submit"><span>Submit</span></button>
|
||||
</form>
|
||||
|
||||
<form id="_contenteditableForm">
|
||||
<div name="first_name" id="first_name" contenteditable placeholder="First Name"></div>
|
||||
<br>
|
||||
|
||||
Reference in New Issue
Block a user