Skip to content

Commit 2fad8ab

Browse files
authored
feat(isLicensePlate): add support for Swedish license plates (validatorjs#1665)
* feat: add support for Swedish license plates * feat: allow spaces * fix(docs): sort alphabetically * fix(isLicensePlate): Trim, disallow 0 and fix regex
1 parent ff4cdfe commit 2fad8ab

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Validator | Description
140140
**isJWT(str)** | check if the string is valid JWT token.
141141
**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.
142142
**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.
143-
**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['cs-CZ', 'de-DE', 'de-LI', 'fi-FI', pt-PT', 'sq-AL', 'pt-BR']` or `any`)
143+
**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['cs-CZ', 'de-DE', 'de-LI', 'fi-FI', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE']` or `any`)
144144
**isLocale(str)** | check if the string is a locale
145145
**isLowercase(str)** | check if the string is lowercase.
146146
**isMACAddress(str [, options])** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{no_separators: false}`. If `no_separators` is true, the validator will allow MAC addresses without separators. 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'. The options also allow a `eui` property to specify if it needs to be validated against EUI-48 or EUI-64. The accepted values of `eui` are: 48, 64.

src/lib/isLicensePlate.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const validators = {
1313
/^[A-Z]{2}[- ]?((\d{3}[- ]?(([A-Z]{2})|T))|(R[- ]?\d{3}))$/.test(str),
1414
'pt-BR': str =>
1515
/^[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str),
16+
'sv-SE': str =>
17+
/^[A-HJ-PR-UW-Z]{3} ?[\d]{2}[A-HJ-PR-UW-Z1-9]$|(^[A-ZÅÄÖ ]{2,7}$)/.test(str.trim()),
1618
};
1719

1820
export default function isLicensePlate(str, locale) {

test/validators.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11767,6 +11767,44 @@ describe('Validators', () => {
1176711767
'FS AB 1234 A',
1176811768
],
1176911769
});
11770+
test({
11771+
validator: 'isLicensePlate',
11772+
args: ['sv-SE'],
11773+
valid: [
11774+
'ABC 123',
11775+
'ABC 12A',
11776+
'ABC123',
11777+
'ABC12A',
11778+
'A WORD',
11779+
'WORD',
11780+
'ÅSNA',
11781+
'EN VARG',
11782+
'CERISE',
11783+
'AA',
11784+
'ABCDEFG',
11785+
'ÅÄÖ',
11786+
'ÅÄÖ ÅÄÖ',
11787+
],
11788+
invalid: [
11789+
'',
11790+
' ',
11791+
'IQV 123',
11792+
'IQV123',
11793+
'ABI 12Q',
11794+
'ÅÄÖ 123',
11795+
'ÅÄÖ 12A',
11796+
'AB1 A23',
11797+
'AB1 12A',
11798+
'lower',
11799+
'abc 123',
11800+
'abc 12A',
11801+
'abc 12a',
11802+
'AbC 12a',
11803+
'WORDLONGERTHANSEVENCHARACTERS',
11804+
'A',
11805+
'ABC-123',
11806+
],
11807+
});
1177011808
});
1177111809
it('should validate VAT numbers', () => {
1177211810
test({

0 commit comments

Comments
 (0)