How to get Country Code from (intl-tel-input) plugin after submitting the form?

How to get Country Code from (intl-tel-input) plugin after submitting the form?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting Country Codes from intl-tel-input on Form Submission

The intl-tel-input plugin solves a specific problem: it standardizes how users input international phone numbers by dynamically updating the country code prefix as they type. When you configure the plugin with separateDialCode: true, the UI renders the +XX prefix distinct from the local number. This separation is crucial for UX, but it introduces a common friction point during data persistence: retrieving that combined value when the form submits.

The core misunderstanding often stems from how the input element behaves visually versus how it behaves programmatically. Even though the user sees two distinct blocks (the code and the number), the underlying DOM element typically concatenates them into a single string unless you explicitly manipulate it. If your database schema expects the full international E.164 format (e.g., +249123456789), relying on the raw input value might result in just the local number or an inconsistent format depending on user interaction.

To retrieve the complete number including the country code upon form submission, you need to access the plugin's internal state rather than reading the value attribute directly. The plugin exposes a method specifically designed for this: .getNumber(). This method returns the number in international format, ensuring that the +XX prefix is included regardless of whether it was typed or selected from the dropdown.

Consider a standard jQuery setup where you initialize the plugin on a form field:

$("#telephone").intlTelInput({ 
    initialCountry: "sd",
    separateDialCode: true, 
    preferredCountries: ['us', 'gb', 'de'] // Optional: limit list length
});

When the form submits, your handler should not simply read $('#telephone').val(). Instead, you must call the getter method. Here is how you handle the submission logic to ensure data integrity before sending it to your MySQL database:

$(document).on('submit', '#contactForm', function(e) {
    e.preventDefault(); // Prevent default submit for demonstration
    
    // Access the plugin instance and retrieve the full number
    const phoneNumber = $('#telephone').intlTelInput('getNumber');
    
    // Validate that a number exists before processing
    if (!phoneNumber) {
        alert('Please enter a valid phone number.');
        return;
    }

    // Store in a model or send to backend
    console.log('Full International Number:', phoneNumber); 
    // Example: Send via AJAX
    // $.post('/api/save-contact', { phone: phoneNumber });
});

Using .intlTelInput('getNumber') guarantees that you get the formatted string ready for storage. If you are using a backend framework like Laravel, you would pass this value directly to your Eloquent model or validation array. It is important to note that getNumber() respects the separateDialCode setting; it will not strip the prefix even if the UI separates them visually.

However, there is a significant pitfall to watch out for: the difference between .getNumber() and .getSelectedCountry().dialCode. If your application logic requires separating the country code from the local number—for instance, to display them differently or to perform specific routing logic—you must use distinct methods. But for storage in a single phone column, getNumber() is the safest choice because it handles the normalization internally.

Another consideration involves the utils.js script often bundled with the plugin. This utility provides functions like formatNumber, but these are primarily for display purposes and might not always align with strict database requirements. Always prefer the instance method .intlTelInput('getNumber') for data extraction, as it is the authoritative source of truth defined by the library's architecture.

Finally, ensure that your server-side validation mirrors this expectation. If you are storing the number in MySQL, consider normalizing the input to E.164 format on the backend as a secondary layer of safety. While intl-tel-input does an excellent job at the client side, server-side normalization prevents edge cases where JavaScript is disabled or the plugin fails to initialize correctly during form submission.

Our network