Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isValidNumber doesn't works properly for Brazilian numbers #1631

Closed
matias-carpintini opened this issue May 23, 2024 · 3 comments
Closed

isValidNumber doesn't works properly for Brazilian numbers #1631

matias-carpintini opened this issue May 23, 2024 · 3 comments

Comments

@matias-carpintini
Copy link

I'm using the latest version of intl-tel-input (v23.0.9) but, when i try to validate a Brazilian phone number (+55 47 00000000) it returns false (not valid).

The placeholder seems to be correct:
Screenshot 2024-05-23 at 2 28 33 PM

And it auto-format as a type.

Here's the full code:

import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
  static targets = ["field"];
  iti = null;

  connect() {
    const input = this.fieldTarget;
    this.iti = intlTelInput(input, {
      initialCountry: "auto",
      geoIpLookup: this.getIp,
      utilsScript:
        "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/23.0.9/js/utils.js",
      hiddenInput: input.dataset.hiddeninput,
      preferredCountries: ["ar", "mx", "br", "cl", "in", "id", "tr"],
    });

    input.addEventListener("input", () => {
      const formattedNumber = this.iti.getNumber(
        intlTelInputUtils.numberFormat.NATIONAL
      );
      input.value = intlTelInputUtils.formatNumber(
        formattedNumber,
        this.iti.getSelectedCountryData().iso2,
        intlTelInputUtils.numberFormat.NATIONAL
      );
    });
  }

  getIp(callback) {
    fetch("https://ipinfo.io/json?token=asdsad", {
      headers: { Accept: "application/json" },
    })
      .then((resp) => resp.json())
      .catch(() => {
        return {
          country: "ar",
        };
      })
      .then((resp) => callback(resp.country));
  }

  validatePhoneNumber() {
    const isValidNumber = this.iti.isValidNumber();
    if (this.iti.getNumber() && !isValidNumber) {
      alert("Not valid!");
      return false;
    }
    return true;
  }
}
@jackocnr
Copy link
Owner

We use libphonenumber for formatting, validation and generating placeholder numbers - if your issue relates to one of these things, please check their test site first and if you see the problem there please file an issue with them instead.

If it's working correctly in libphonenumber, the next step is to check that we're using the latest version of libphonenumber in the plugin - check the version history here. If we need to upgrade, it only takes 5 minutes to do this yourself and submit a Pull Request - instructions here: Updating to a new version of libphonenumber.

@jackocnr
Copy link
Owner

A few extra notes:

  • preferredCountries option has been removed. See countryOrder option instead.
  • if you're using ES modules (import/export) then you should import the plugin with import intlTelInput from "intl-tel-input"
  • everything inside your "input" event listener should be redundant - we have the formatAsYouType option enabled by default now, so you should be able to drop all of that
  • intlTelInputUtils is now intlTelInput.utils

@matias-carpintini
Copy link
Author

It worked like a charm! Thank you so much :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants