phone-validate
v1.0.13
Published
`phone-validate` is a simple library to validate and format phone numbers
Downloads
8
Readme
phone-validate
is a simple library to validate and format phone numbers
Installation
To install the library, use npm:
npm install phone-validate
Usage
The library provides two main functions: validPhoneToBool
and validPhoneToStr
.
validPhoneToBool
This function checks if a given phone number matches a specified pattern.
Parameters
- regExp: string: The regular expression pattern to match against. Use X for digits and spaces for separators.
- numStr: string: The phone number string to validate.
Returns boolean. True if the phone number matches the pattern, false otherwise.
Example
import { validPhoneToBool } from "phone-validate";
const pattern = "+9(XXX)-XXX-XX-XX";
const phoneNumber = "+9(979)-446-85-39";
const isValid = validPhoneToBool(pattern, phoneNumber);
console.log(isValid); // true
validPhoneToStr
This function formats a given phone number to match a specified pattern, or returns a truncated version if it doesn't match.
Parameters
- regExp: string: The regular expression pattern to match against. Use X for digits and spaces for separators.
- numStr: string: The phone number string to validate.
Returns string. The formatted phone number if it matches the pattern, or a truncated string.
import { validPhoneToStr } from "phone-validate";
const pattern = "+9(XXX)-XXX-XX-XX";
const phoneNumber = "+9(979)-446-85-39";
const formattedPhoneNumber = validPhoneToStr(pattern, phoneNumber);
console.log(formattedPhoneNumber); // "+9(979)-446-85-39"
If the phone number is not valid:
import { validPhoneToStr } from "phone-validate";
const pattern = "+9(XXX)-XXX-XX-XX";
const phoneNumber = "+9(979)-446-85-39G";
const formattedPhoneNumber = validPhoneToStr(pattern, phoneNumber);
console.log(formattedPhoneNumber); // "+9(979)-446-85-39"
Assumed to be a function that is used to validate the input information in the <input>