US Phone Number Validation Function Documentation

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.

Import

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 Signature

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

Parameters

Default Error Messages

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

Examples

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' }