Phone Number Validation Function Documentation

The validatePhoneNumber Function is used to validate phone numbers. It returns an object with two properties: "isValid" (boolean) and "errorMsg" (string). The "isValid" property will be true if the phone number meets the specified criteria, and "errorMsg" will contain the error message if the phone number is invalid, or it will be null if the phone number is valid.

Function Signature

function validatePhoneNumber(
	phoneNumber: string,
	errorMsg?: string[]
): { isValid: boolean, errorMsg: string | null };

Parameters

Default Error Messages

[
	'Invalid value passed',
	'Invalid phone number',
	'Unknown error',
]

Examples

const result1 = validatePhoneNumber('555-123-4567');
console.log(result1);
// Output: { isValid: false, errorMsg: 'Invalid phone number' }

const customErrorMsg = [null, 'Custom error 2'];
const result2 = validatePhoneNumber('(555) 123-4567', customErrorMsg);
console.log(result2);
// Output: { isValid: false, errorMsg: 'Custom error 2' }