The validateUSPhoneNumber
function is used to validate US phone numbers. It supports various formats, including "XXX-XXX-XXXX", "(XXX) XXX-XXXX", and "1 (XXX) XXX-XXXX". It returns an object with two properties: "isValid" (boolean) and "errorMsg" (string). The "isValid" property will be true if the phone number is valid, and "errorMsg" will contain the error message if the phone number is invalid, or it will be null if the phone number is valid.
The function can be imported using ES6 syntax from the "multiform-validator" package:
import { validateUSPhoneNumber } from 'multiform-validator';
Alternatively, you can import the function using CommonJS syntax with require
(Node.js):
const { validateUSPhoneNumber } = require('multiform-validator');
function validateUSPhoneNumber(
phoneNumber: string,
errorMsg?: string[]
): { isValid: boolean, errorMsg: string | null };
phoneNumber
(string) - The US 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 = validateUSPhoneNumber('555-123-4567');
console.log(result1);
// Output: { isValid: true, errorMsg: null }
const customErrorMsg = ['Custom error 1', 'Custom error 2'];
const result2 = validateUSPhoneNumber('(555) 123-4567', customErrorMsg);
console.log(result2);
// Output: { isValid: false, errorMsg: 'Invalid phone number' }