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 validatePhoneNumber(
phoneNumber: string,
errorMsg?: string[]
): { isValid: boolean, errorMsg: string | null };
phoneNumber
(string) - The phone number to be validated.errorMsg
(string[]) [optional] - An array of error messages to customize the response. If not provided, the function will use default error messages.[
'Invalid value passed',
'Invalid phone number',
'Unknown error',
]
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' }