Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
libphonenumber-js evaluates good phone number as false

I'm stumped.

I've messed around with this for a while and it doesn't make sense.

libphonenumber-js does not validate a phone number that should be a valid number. It returns 'false' every time.

This seems pretty simple and straightforward. A 10 digit number from a form field.

const phoneNumber = libphonenumber.parsePhoneNumber(contact_asset, 'US')

        if (phoneNumber) {
            console.log(libphonenumber.isPossiblePhoneNumber(JSON.stringify(phoneNumber)));
            console.log(phoneNumber.formatNational());

}

What seems odd to me is that when I parse the phone number and pass it to the validator, if it's not a string, it complains. I would expect libphonenumber to accept the output of parse without stringifying it.

What am I missing?

https://jsfiddle.net/TexEvans/sxL28j7h/4/

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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());
}
about 4 years ago · Juan Pablo Isaza Relatório

0

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'));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda