Skip to content

Commit e882576

Browse files
License Plate validators: pt-BR and mercosur
1 parent 1b85829 commit e882576

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Validator | Description
131131
**isJWT(str)** | check if the string is valid JWT token.
132132
**isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.<br/><br/>`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format.
133133
**isLength(str [, options])** | check if the string's length falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
134-
**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['de-DE', 'de-LI', 'pt-PT', 'sq-AL']` or `any`)
134+
**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['de-DE', 'de-LI', 'pt-PT', 'sq-AL', 'pt-BR', 'mercosur']` or `any`).
135135
**isLocale(str)** | check if the string is a locale
136136
**isLowercase(str)** | check if the string is lowercase.
137137
**isMACAddress(str)** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{no_colons: false}`. If `no_colons` is true, the validator will allow MAC addresses without the colons. Also, it allows the use of hyphens, spaces or dots e.g '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'.

src/lib/isLicensePlate.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const validators = {
88
/^([A-Z]{2}|[0-9]{2})[ -·]?([A-Z]{2}|[0-9]{2})[ -·]?([A-Z]{2}|[0-9]{2})$/.test(str),
99
'sq-AL': str =>
1010
/^[A-Z]{2}[- ]?((\d{3}[- ]?(([A-Z]{2})|T))|(R[- ]?\d{3}))$/.test(str),
11+
'pt-BR': str =>
12+
/^[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str),
13+
mercosur: str =>
14+
/^[A-Z]{2}[ -]?[0-9]{3}[ -]?[A-Z]{2}|[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{4}[ -]?[0-9]{3}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str),
1115
};
1216

1317
export default function isLicensePlate(str, locale) {

test/validators.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10292,6 +10292,56 @@ describe('Validators', () => {
1029210292
'AAA 00 AAA',
1029310293
],
1029410294
});
10295+
test({
10296+
validator: 'isLicensePlate',
10297+
args: ['pt-BR'],
10298+
valid: [
10299+
'ABC1234',
10300+
'ABC 1234',
10301+
'ABC-1234',
10302+
'ABC1D23',
10303+
'ABC1K23',
10304+
'ABC1Z23',
10305+
'ABC 1D23',
10306+
'ABC-1D23',
10307+
],
10308+
invalid: [
10309+
'',
10310+
'AA 0 A',
10311+
'AAA 00 AAA',
10312+
'ABCD123',
10313+
'AB12345',
10314+
'AB123DC',
10315+
],
10316+
});
10317+
test({
10318+
validator: 'isLicensePlate',
10319+
args: ['mercosur'],
10320+
valid: [
10321+
'AB 123 CD',
10322+
'ABC 1A23',
10323+
'ABCD 123',
10324+
'ABC 1234',
10325+
'AB-123-CD',
10326+
'ABC-1A23',
10327+
'ABCD-123',
10328+
'ABC-1234',
10329+
'AB123CD',
10330+
'ABC1A23',
10331+
'ABCD123',
10332+
'ABC1234',
10333+
],
10334+
invalid: [
10335+
'',
10336+
'A0A0A0A0',
10337+
' A A00 ',
10338+
'0101PTQ',
10339+
'01PTQPQ',
10340+
'A1 A23 CD',
10341+
'AB1 AA23',
10342+
'ABCDE 23',
10343+
],
10344+
});
1029510345
test({
1029610346
validator: 'isLicensePlate',
1029710347
args: ['any'],

0 commit comments

Comments
 (0)