Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit 2bf38b9

Browse files
feat(rules): Added MobilePhone rule
1 parent 62ef013 commit 2bf38b9

5 files changed

Lines changed: 221 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
170170
- [Max](docs/max.md)
171171
- [Mimetype](docs/mimetype.md)
172172
- [Min](docs/min.md)
173+
- [MobilePhone](docs/mobile-phone.md)
173174
- [MongoId](docs/mongo-id.md)
174175
- [Multibyte](docs/multibyte.md)
175176
- [Multiple](docs/multiple.md)

docs/mobile-phone.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# MobilePhone
2+
3+
Validates if the given input is a mobile phone.
4+
5+
Valid values:
6+
7+
```js
8+
validator.mobilePhone('pt-BR').validate('55-17-3332-2155');
9+
validator.mobilePhone('en-GB').validate('447789345856');
10+
validator.mobilePhone('en-US').validate('19876543210');
11+
validator.mobilePhone('pl-PL').validate('+48 56 6572724');
12+
```
13+
14+
Invalid values:
15+
16+
```js
17+
validator.mobilePhone('pt-BR').validate('+55012962308');
18+
validator.mobilePhone('en-GB').validate('077389f8688');
19+
validator.mobilePhone('en-US').validate('1437439210');
20+
validator.mobilePhone('pl-PL').validate('3454535');
21+
```
22+
23+
The current supported services are:
24+
25+
- ar-AE
26+
- ar-DZ
27+
- ar-EG
28+
- ar-JO
29+
- ar-SA
30+
- ar-SY
31+
- cs-CZ
32+
- da-DK
33+
- de-DE
34+
- el-GR
35+
- en-AU
36+
- en-CA
37+
- en-GB
38+
- en-HK
39+
- en-IN
40+
- en-KE
41+
- en-NG
42+
- en-NZ
43+
- en-PK
44+
- en-RW
45+
- en-SG
46+
- en-TZ
47+
- en-UG
48+
- en-US
49+
- en-ZA
50+
- en-ZM
51+
- es-ES
52+
- et-EE
53+
- fa-IR
54+
- fi-FI
55+
- fo-FO
56+
- fr-BE
57+
- fr-FR
58+
- he-IL
59+
- hu-HU
60+
- id-ID
61+
- it-IT
62+
- ja-JP
63+
- kl-GL
64+
- ko-KR
65+
- lt-LT
66+
- ms-MY
67+
- nb-NO
68+
- nl-BE
69+
- nn-NO
70+
- pl-PL
71+
- pt-BR
72+
- pt-PT
73+
- ro-RO
74+
- ru-RU
75+
- sk-SK
76+
- sr-RS
77+
- tr-TR
78+
- uk-UA
79+
- vi-VN
80+
- zh-CN
81+
- zh-HK
82+
- zh-TW

src/rules/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export * from './mac-address';
8888
export * from './max';
8989
export * from './mimetype';
9090
export * from './min';
91+
export * from './mobile-phone';
9192
export * from './mongo-id';
9293
export * from './multibyte';
9394
export * from './multiple';

src/rules/mobile-phone.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { AbstractServiceRegex } from './abstract-service-regex';
2+
3+
export class MobilePhone extends AbstractServiceRegex {
4+
5+
/**
6+
* Services.
7+
*/
8+
protected get services(): Map<string, RegExp> {
9+
return new Map([
10+
['ar-AE', /^((\+?971)|0)?5[024568]\d{7}$/],
11+
['ar-DZ', /^(\+?213|0)(5|6|7)\d{8}$/],
12+
['ar-EG', /^((\+?20)|0)?1[012]\d{8}$/],
13+
['ar-JO', /^(\+?962|0)?7[789]\d{7}$/],
14+
['ar-SA', /^(!?(\+?966)|0)?5\d{8}$/],
15+
['ar-SY', /^(!?(\+?963)|0)?9\d{8}$/],
16+
['cs-CZ', /^(\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/],
17+
['da-DK', /^(\+?45)?\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2}$/],
18+
19+
/* tslint:disable max-line-length */
20+
['de-DE', /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/],
21+
/* tslint:enable max-line-length */
22+
23+
['el-GR', /^(\+?30|0)?(69\d{8})$/],
24+
['en-AU', /^(\+?61|0)4\d{8}$/],
25+
['en-CA', /^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$/],
26+
['en-GB', /^(\+?44|0)7\d{9}$/],
27+
['en-HK', /^(\+?852\-?)?[456789]\d{3}\-?\d{4}$/],
28+
['en-IN', /^(\+?91|0)?[789]\d{9}$/],
29+
['en-KE', /^(\+?254|0)?[7]\d{8}$/],
30+
['en-NG', /^(\+?234|0)?[789]\d{9}$/],
31+
['en-NZ', /^(\+?64|0)2\d{7,9}$/],
32+
['en-PK', /^((\+92)|(0092))-{0,1}\d{3}-{0,1}\d{7}$|^\d{11}$|^\d{4}-\d{7}$/],
33+
['en-RW', /^(\+?250|0)?[7]\d{8}$/],
34+
['en-SG', /^(\+65)?[89]\d{7}$/],
35+
['en-TZ', /^(\+?255|0)?[67]\d{8}$/],
36+
['en-UG', /^(\+?256|0)?[7]\d{8}$/],
37+
['en-US', /^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$/],
38+
['en-ZA', /^(\+?27|0)\d{9}$/],
39+
['en-ZM', /^(\+?26)?09[567]\d{7}$/],
40+
['es-ES', /^(\+?34)?(6\d{1}|7[1234])\d{7}$/],
41+
['et-EE', /^(\+?372)?\s?(5|8[1-4])\s?([0-9]\s?){6,7}$/],
42+
['fa-IR', /^(\+?98[\-\s]?|0)9[0-39]\d[\-\s]?\d{3}[\-\s]?\d{4}$/],
43+
['fi-FI', /^(\+?358|0)\s?(4(0|1|2|4|5|6)?|50)\s?(\d\s?){4,8}\d$/],
44+
['fo-FO', /^(\+?298)?\s?\d{2}\s?\d{2}\s?\d{2}$/],
45+
['fr-BE', /^(\+?32|0)4?\d{8}$/],
46+
['fr-FR', /^(\+?33|0)[67]\d{8}$/],
47+
['he-IL', /^(\+972|0)([23489]|5[0248]|77)[1-9]\d{6}/],
48+
['hu-HU', /^(\+?36)(20|30|70)\d{7}$/],
49+
['id-ID', /^(\+?62|0[1-9])[\s|\d]+$/],
50+
['it-IT', /^(\+?39)?\s?3\d{2} ?\d{6,7}$/],
51+
['ja-JP', /^(\+?81|0)[789]0[ \-]?[1-9]\d{2}[ \-]?\d{5}$/],
52+
['kl-GL', /^(\+?299)?\s?\d{2}\s?\d{2}\s?\d{2}$/],
53+
['ko-KR', /^((\+?82)[ \-]?)?0?1([0|1|6|7|8|9]{1})[ \-]?\d{3,4}[ \-]?\d{4}$/],
54+
['lt-LT', /^(\+370|8)\d{8}$/],
55+
['ms-MY', /^(\+?6?01){1}(([145]{1}(\-|\s)?\d{7,8})|([236789]{1}(\s|\-)?\d{7}))$/],
56+
['nb-NO', /^(\+?47)?[49]\d{7}$/],
57+
['nl-BE', /^(\+?32|0)4?\d{8}$/],
58+
['nn-NO', /^(\+?47)?[49]\d{7}$/],
59+
['pl-PL', /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/],
60+
['pt-BR', /^(\+?55|0)\-?[1-9]{2}\-?[2-9]{1}\d{3,4}\-?\d{4}$/],
61+
['pt-PT', /^(\+?351)?9[1236]\d{7}$/],
62+
['ro-RO', /^(\+?4?0)\s?7\d{2}(\/|\s|\.|\-)?\d{3}(\s|\.|\-)?\d{3}$/],
63+
['ru-RU', /^(\+?7|8)?9\d{9}$/],
64+
['sk-SK', /^(\+?421)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/],
65+
['sr-RS', /^(\+3816|06)[- \d]{5,9}$/],
66+
['tr-TR', /^(\+?90|0)?5\d{9}$/],
67+
['uk-UA', /^(\+?38|8)?0\d{9}$/],
68+
['vi-VN', /^(\+?84|0)?((1(2([0-9])|6([2-9])|88|99))|(9((?!5)[0-9])))([0-9]{7})$/],
69+
['zh-CN', /^(\+?0?86\-?)?1[345789]\d{9}$/],
70+
['zh-HK', /^(\+?852\-?)?[456789]\d{3}\-?\d{4}$/],
71+
['zh-TW', /^(\+?886\-?|0)?9\d{8}$/]
72+
]);
73+
}
74+
}
75+
76+
export default MobilePhone;

test/rules/mobile-phone.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { assert } from 'chai';
2+
3+
import { AbstractRule } from '../../src/rules/abstract-rule';
4+
import { MobilePhone } from '../../src/rules/mobile-phone';
5+
6+
describe('MobilePhone', () => {
7+
8+
let mobilePhone: MobilePhone;
9+
10+
beforeEach(() => {
11+
mobilePhone = new MobilePhone();
12+
});
13+
14+
it('is rule', () => {
15+
assert.instanceOf(mobilePhone, AbstractRule);
16+
});
17+
18+
it('values is valid', () => {
19+
assert.isTrue(new MobilePhone('pt-br').validate('55-17-3332-2155'));
20+
assert.isTrue(new MobilePhone('pt-br').validate('01523456987'));
21+
assert.isTrue(new MobilePhone('en-gb').validate('447789345856'));
22+
assert.isTrue(new MobilePhone('en-gb').validate('07888814488'));
23+
assert.isTrue(new MobilePhone('en-US').validate('19876543210'));
24+
assert.isTrue(new MobilePhone('en-US').validate('8005552222'));
25+
assert.isTrue(new MobilePhone('pl-pl').validate('+48512689767'));
26+
assert.isTrue(new MobilePhone('pl-pl').validate('657562855'));
27+
assert.isTrue(new MobilePhone('pt-br', 'en-us').validate('022995678947'));
28+
assert.isTrue(new MobilePhone('pt-br', 'en-gb').validate('07888814488'));
29+
30+
assert.isTrue(mobilePhone.validate('551223456789'));
31+
assert.isTrue(mobilePhone.validate('447789345856'));
32+
assert.isTrue(mobilePhone.validate('+15673628910'));
33+
assert.isTrue(mobilePhone.validate('551223456789'));
34+
});
35+
36+
it('values is not valid', () => {
37+
assert.isFalse(new MobilePhone('pt-br').validate('657562855'));
38+
assert.isFalse(new MobilePhone('en-gb').validate('0898123'));
39+
assert.isFalse(new MobilePhone('en-us').validate('07888814488'));
40+
assert.isFalse(new MobilePhone('pl-pl').validate('55-17-3332-2155'));
41+
assert.isFalse(new MobilePhone('pt-br', 'en-us').validate('0898123'));
42+
43+
assert.isFalse(mobilePhone.validate(''));
44+
assert.isFalse(mobilePhone.validate('foo'));
45+
assert.isFalse(mobilePhone.validate('&stR@ng3|) (|-|@r$'));
46+
assert.isFalse(mobilePhone.validate(1.0));
47+
assert.isFalse(mobilePhone.validate('wrongtld'));
48+
assert.isFalse(mobilePhone.validate(null));
49+
assert.isFalse(mobilePhone.validate(undefined));
50+
assert.isFalse(mobilePhone.validate(true));
51+
assert.isFalse(mobilePhone.validate(false));
52+
assert.isFalse(mobilePhone.validate([]));
53+
assert.isFalse(mobilePhone.validate({}));
54+
assert.isFalse(mobilePhone.validate(new Array('foo')));
55+
assert.isFalse(mobilePhone.validate(new Object({foo: 'bar'})));
56+
57+
class Foo {}
58+
assert.isFalse(mobilePhone.validate(new Foo()));
59+
});
60+
61+
});

0 commit comments

Comments
 (0)