You should always take a look at the documention
parsePhoneNumber returns …an instance of PhoneNumber class, or undefined if no phone number could be parsed…. And you can't expect that JSON.stringify(phoneNumber) will result in something that just contains a phone number.
And isPossiblePhoneNumber expects a string as parameter that is a valid phone number; the documentation states there:
This function is just a shortcut for a two-step process of "strictly" parsing a phone number and then calling
.isPossible().
So what you want to do is either:
const contact_asset = "2022032034"
const phoneNumber = libphonenumber.parsePhoneNumber(contact_asset, 'US')
if (phoneNumber) {
console.log(phoneNumber.isPossible());
}
Or
const contact_asset = "2022032034"
console.log(libphonenumber.isPossiblePhoneNumber(contact_asset, 'US'));
Answer from t.niese on Stack Overflow
» npm install libphonenumber-js
libphonenumber-js evaluates good phone number as false
What happened to libphonenumber-js?
ES6 imports with libphonenumber-js
reactjs - How to validate the phone number using react - Stack Overflow
You should always take a look at the documention
parsePhoneNumber returns …an instance of PhoneNumber class, or undefined if no phone number could be parsed…. And you can't expect that JSON.stringify(phoneNumber) will result in something that just contains a phone number.
And isPossiblePhoneNumber expects a string as parameter that is a valid phone number; the documentation states there:
This function is just a shortcut for a two-step process of "strictly" parsing a phone number and then calling
.isPossible().
So what you want to do is either:
const contact_asset = "2022032034"
const phoneNumber = libphonenumber.parsePhoneNumber(contact_asset, 'US')
if (phoneNumber) {
console.log(phoneNumber.isPossible());
}
Or
const contact_asset = "2022032034"
console.log(libphonenumber.isPossiblePhoneNumber(contact_asset, 'US'));
If I format it to international and then check if it's valid, its returning me a valid number, check if this helps.
let contact_asset = "2022032034"
const phoneNumber = libphonenumber.parsePhoneNumber(contact_asset, 'US')
const formattedPhoneNumber = phoneNumber.formatInternational()
if (phoneNumber) {
console.log(libphonenumber.isValidPhoneNumber(formattedPhoneNumber));
console.log(libphonenumber.isPossiblePhoneNumber(formattedPhoneNumber));
console.log(phoneNumber.formatNational());
}
Does anyone knows what happened to this project? libphonenumber-js
» npm install react-phone-number-input
You can use npm package 'validator'.
npm install validator --save
then
import validator from 'validator'
validatePhoneNumber = (number) => {
const isValidPhoneNumber = validator.isMobilePhone(number)
return (isValidPhoneNumber)
}
console.log(validatePhoneNumber('react')) //=> false
console.log(validatePhoneNumber('0641732123')) //=> true
you can also set the locale for specific countries (eg. 'en-ZA') and more as your options.
docs : https://www.npmjs.com/package/validator
If you don't have any problem to use any npm package then you can use
https://catamphetamine.gitlab.io/react-phone-number-input/
package. With this package, you can easily validate any phone number.