21 lines
715 B
TypeScript
21 lines
715 B
TypeScript
export function isValidPhoneNumber(value: string): boolean {
|
|
let compact = '';
|
|
for (const character of value.trim()) {
|
|
if (!' \t\n\r()./-'.includes(character)) compact += character;
|
|
}
|
|
if (!compact) return true;
|
|
if (compact.length === 10 && compact[0] === '0' && digits(compact, 0)) return true;
|
|
if (compact.startsWith('00')) compact = `+${compact.slice(2)}`;
|
|
return (
|
|
compact.startsWith('+') && compact.length >= 9 && compact.length <= 16 && digits(compact, 1)
|
|
);
|
|
}
|
|
|
|
function digits(value: string, start: number): boolean {
|
|
for (let index = start; index < value.length; index += 1) {
|
|
const character = value.charCodeAt(index);
|
|
if (character < 48 || character > 57) return false;
|
|
}
|
|
return true;
|
|
}
|