ozansozuoz
ozansozuoz

Reputation: 237

Wrong use of CDN (libphonenumber-js) or CDN not working?

I am trying to use the library libphonenumber-js.

<body>

    <script src="https://unpkg.com/[email protected]/bundle/libphonenumber-min.js"></script>

    <script src="./js/main.js"></script>

</body>

In my main.js I tried to write a simple code which is from the docs of libphonenumber-js:

    const phoneNumber = parsePhoneNumber('+12133734253');

However I get this error. Same thing with other functions from the docs.

Uncaught ReferenceError: parsePhoneNumber is not defined

In my network tabs I can see that the min.js file is being downloaded. But its functions are not usable for some reason. I tried making a min.js file and using it as well but with not luck.

I also tried many different CDN services which also host this library.

Upvotes: 1

Views: 1650

Answers (2)

Saad Masood
Saad Masood

Reputation: 332

This fixed it for me, But since it still returns 3 digits, which causes the field to change, triggering the event again and making it auto-format right back into what it was.

if (value.includes('(') && !value.includes(')')) {
      const tempValue = value.replace('(', '');
      return tempValue.slice(0, tempValue.length - 1);
}

Upvotes: 0

ozansozuoz
ozansozuoz

Reputation: 237

For anyone wondering:

I found out in the issues page of the repo that you have to use window.libphonenumber or just libphonenumber in front of the function. So

window.libphonenumber.parsePhoneNumberFromString(
      '(213) 373-42-53 ext. 1234',
      'US'
    );

Also in my example turns out that the correct function was parsePhoneNumberFromString

Upvotes: 5

Related Questions