@@ -5,12 +5,14 @@ import { parsePhoneNumberFromString, CountryCode } from 'libphonenumber-js';
5
5
export const IS_PHONE_NUMBER = 'isPhoneNumber' ;
6
6
7
7
/**
8
- * Checks if the string is a valid phone number.
8
+ * Checks if the string is a valid phone number. To successfully validate any phone number the text must include
9
+ * the intl. calling code, if the calling code wont be provided then the region must be set.
10
+ *
9
11
* @param value the potential phone number string to test
10
- * @param { string } region 2 characters uppercase country code (e.g. DE, US, CH).
11
- * If users must enter the intl. prefix (e.g. +41), then you may pass "ZZ" or null as region .
12
+ * @param region 2 characters uppercase country code (e.g. DE, US, CH) for country specific validation .
13
+ * If text doesn't start with the international calling code (e.g. +41), then you must set this parameter .
12
14
*/
13
- export function isPhoneNumber ( value : string , region : CountryCode | undefined ) : boolean {
15
+ export function isPhoneNumber ( value : string , region ? : CountryCode ) : boolean {
14
16
try {
15
17
const phoneNum = parsePhoneNumberFromString ( value , region ) ;
16
18
const result = phoneNum ?. isValid ( ) ;
@@ -22,14 +24,13 @@ export function isPhoneNumber(value: string, region: CountryCode | undefined): b
22
24
}
23
25
24
26
/**
25
- * Checks if the string is a valid phone number.
26
- * @param region 2 characters uppercase country code (e.g. DE, US, CH).
27
- * If users must enter the intl. prefix (e.g. +41), then you may pass "ZZ" or null as region.
27
+ * Checks if the string is a valid phone number. To successfully validate any phone number the text must include
28
+ * the intl. calling code, if the calling code wont be provided then the region must be set.
29
+ *
30
+ * @param region 2 characters uppercase country code (e.g. DE, US, CH) for country specific validation.
31
+ * If text doesn't start with the international calling code (e.g. +41), then you must set this parameter.
28
32
*/
29
- export function IsPhoneNumber (
30
- region : CountryCode | undefined ,
31
- validationOptions ?: ValidationOptions
32
- ) : PropertyDecorator {
33
+ export function IsPhoneNumber ( region ?: CountryCode , validationOptions ?: ValidationOptions ) : PropertyDecorator {
33
34
return ValidateBy (
34
35
{
35
36
name : IS_PHONE_NUMBER ,
0 commit comments