Skip to content

Commit af27e24

Browse files
committed
phonenumber validator
1 parent 1043391 commit af27e24

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

packages/validation/src/no.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,22 @@
55
export function postalCodeValidator(value: string): boolean {
66
return /^\d{4}$/.test(value);
77
}
8+
9+
type PhoneNumberOptions = {
10+
mobileOnly?: boolean;
11+
};
12+
13+
export function phoneNumberValidator(
14+
value: string,
15+
options: PhoneNumberOptions = {},
16+
): boolean {
17+
const isPhoneNumber = /^\d{8}$/.test(value);
18+
19+
if (options.mobileOnly) {
20+
// Norwegian mobile phone numbers start with 4 or 9
21+
// See https://nkom.no/telefoni-og-telefonnummer/telefonnummer-og-den-norske-nummerplan/alle-nummerserier-for-norske-telefonnumre
22+
return isPhoneNumber && ['4', '9'].includes(value.charAt(0));
23+
}
24+
25+
return isPhoneNumber;
26+
}

packages/validation/src/se.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,21 @@
55
export function postalCodeValidator(value: string): boolean {
66
return /^\d{3} ?\d{2}$/.test(value);
77
}
8+
9+
type PhoneNumberOptions = {
10+
mobileOnly?: boolean;
11+
};
12+
13+
export function phoneNumberValidator(
14+
value: string,
15+
options: PhoneNumberOptions = {},
16+
): boolean {
17+
if (options.mobileOnly) {
18+
const isMobileNumber = /^07\d{8}$/.test(value);
19+
return isMobileNumber;
20+
}
21+
22+
const isPhoneNumber = /^0\d{7,9}$/.test(value);
23+
24+
return isPhoneNumber;
25+
}

packages/validation/src/validation.test.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { describe, expect, test } from 'vitest';
2-
import { postalCodeValidator as postalCodeValidatorNo } from './no';
3-
import { postalCodeValidator as postalCodeValidatorSe } from './se';
2+
import {
3+
phoneNumberValidator as phoneNumberValidatorNo,
4+
postalCodeValidator as postalCodeValidatorNo,
5+
} from './no';
6+
import {
7+
phoneNumberValidator as phoneNumberValidatorSe,
8+
postalCodeValidator as postalCodeValidatorSe,
9+
} from './se';
410

511
describe('Norwegian', () => {
612
describe('postalCodeValidator()', () => {
@@ -13,6 +19,23 @@ describe('Norwegian', () => {
1319
expect(postalCodeValidatorNo('not a number')).toBeFalsy();
1420
});
1521
});
22+
23+
describe('phoneNumberValidator()', () => {
24+
test('validates phone numbers', () => {
25+
expect(phoneNumberValidatorNo('22865500')).toBeTruthy();
26+
expect(phoneNumberValidatorNo('228655000')).toBeFalsy();
27+
28+
expect(
29+
phoneNumberValidatorNo('40 00 00 00', { mobileOnly: true }),
30+
).toBeTruthy();
31+
expect(
32+
phoneNumberValidatorNo('99 99 99 99', { mobileOnly: true }),
33+
).toBeTruthy();
34+
expect(
35+
phoneNumberValidatorNo('22865500', { mobileOnly: true }),
36+
).toBeFalsy();
37+
});
38+
});
1639
});
1740

1841
describe('Swedish', () => {
@@ -27,4 +50,26 @@ describe('Swedish', () => {
2750
expect(postalCodeValidatorSe('not a number')).toBeFalsy();
2851
});
2952
});
53+
54+
describe('phoneNumberValidator()', () => {
55+
test('validates phone numbers', () => {
56+
// should be 8 to 10 digits
57+
expect(phoneNumberValidatorSe('08123456')).toBeTruthy();
58+
expect(phoneNumberValidatorSe('031123456')).toBeTruthy();
59+
expect(phoneNumberValidatorSe('0311234567')).toBeTruthy();
60+
61+
// too short
62+
expect(phoneNumberValidatorSe('0812345')).toBeFalsy();
63+
// too long
64+
expect(phoneNumberValidatorSe('0303123456789')).toBeFalsy();
65+
66+
// cannot start with something other than 0
67+
expect(phoneNumberValidatorSe('12345678')).toBeFalsy();
68+
69+
// A Swedish mobile number is always 10 digits and starts with 07
70+
expect(
71+
phoneNumberValidatorSe('0712345678', { mobileOnly: true }),
72+
).toBeTruthy();
73+
});
74+
});
3075
});

0 commit comments

Comments
 (0)