Angular 8 input phone number with country code

Β·

, ,
Angular 8 input phone number with country code

Hello to all, welcome to therichpost.com. In this post, I will tell you, Angular 8 input phone number with country code.

Guy’s this post code snippet will also work in angular latest version Angular 17 input type phone number with country code and country flags.

Angular 8 input phone number with country code
Angular 8 input phone number with country code
Angular Input with Country Code Working Video

Guys click here to see the updated version of this post for Angular 11+ versions.

Post Working:

In this post, I am showing input field with enter phone number with country code and country flags.

Here is the working coding steps and please follow carefully:

1. Here are the basics commands to install angular 17 on your system:

npm install -g @angular/cli 

ng new angularpopup //Create new Angular Project

cd angularpopup // Go inside the Angular Project Folder

ng serve --open // Run and Open the Angular Project

http://localhost:4200/ // Working Angular Project Url

2. Here is the below command you need to run into your terminal to add intl tel input into your angular 8 application:

npm install ng2-tel-input intl-tel-input --save

3. Now you need to add below code into your angular.json file:

...
"styles": ["node_modules/intl-tel-input/build/css/intlTelInput.css"],
"scripts": ["node_modules/intl-tel-input/build/js/intlTelInput.min.js"]
...

4. Now you need to add below code into your src/app/app.module.ts file:

...
import {Ng2TelInputModule} from 'ng2-tel-input';
...

imports: [...  Ng2TelInputModule ...]

5. Now you need to add below code into your src/app/app.component.html file:

<input type="text" ng2TelInput />

This is it guy’s and if you will have any kind of query related to this post then feel free to comment below.

Note: Friends, In this post, I just tell the basic setup and things, you can change the code according to your requirements.

Jassa

Thank you

Comments

69 responses to “Angular 8 input phone number with country code”

  1. Adnan Ahmed Avatar
    Adnan Ahmed

    Hi , I the code you provided above is working properly, if you have src/app/app.component.ts code sp, please share with me.

  2. Ajay Malhotra Avatar

    Hello and why you need app.component.ts file?

  3. nandan Avatar
    nandan

    Do we need to define some code on app.component.ts too you should also mention that code in order to make it complete

  4. Ajay Malhotra Avatar

    No need but if you want to save or send the info then.

  5. Nemanja Avatar
    Nemanja

    Code just doesnt work.

  6. Ajay Malhotra Avatar

    Did you follow proper tut?

  7. Anas Avatar
    Anas

    window.intlTelInput is not a function
    at Ng2TelInput.push

    Not Working .

  8. Ajay Malhotra Avatar

    I will check and update.

  9. TheI Avatar
    TheI

    I tried with that ionic framework / Angular. Its not working. Any Idea dude?

  10. Ajay Malhotra Avatar

    Hi, but for me its working and you can check the video on youtube also.

  11. Alauddin Jahin Avatar
    Alauddin Jahin

    Hello,
    I have been trying to do it as well but I am getting error in my angular 9 project and also I have checked npm packages all documentation . After all I am getting same error . Error : this .telInput is undefine. some times it show like this . telInput in not a function . Tell me the solution What will I do for solving this problem. Why I am facing this type of error.

    and I have seen your toast posted too and after doing all I am not getting the toast design but it’s working. What should I do to solve it?

  12. Ajay Malhotra Avatar

    I will update you.

  13. Shraddha Avatar
    Shraddha

    I want to add validation error messages for above code

  14. Shraddha Avatar
    Shraddha

    Can u share link of video that validation handled for this code

  15. Ajay Malhotra Avatar

    Yes and I will share the code.
    Thank you

  16. Akhil Jain Avatar
    Akhil Jain

    how to update this country code in my form value.

  17. Ajay Malhotra Avatar

    Here is the code:
    add this attribute inside input
    [ng2TelInputOptions]=”{initialCountry: β€˜in’}”

  18. Akhil Jain Avatar
    Akhil Jain

    this is the initial set but when I update or add like I choose the “butan” country and save it then I checked or update the “butan” will not be saved.so how to save the country flag in the form.

  19. Ajay Malhotra Avatar

    For that, you need to just country code with number with form posting.

  20. Akhil Jain Avatar
    Akhil Jain

    can u provide me the code how to do that?

  21. Ajay Malhotra Avatar

    I will update you on this.

  22. Dhairya Avatar
    Dhairya

    How to get both country code values and mobile number and validate
    also how to how country code dropdown in edit mode when user choose number according to country

  23. Shiva Avatar
    Shiva

    Hi Ajay, when i make the then its giving me below error-

    ERROR DOMException: Failed to execute ‘setSelectionRange’ on ‘HTMLInputElement’: The input element’s type (‘number’) does not support selection.

    and cant able to store the data in backed because of this error, can you help me how to resolve this problem?
    i want to accept the mobile number type as a number not a text?(in tutorial you make input type=”text”)
    can you help me with this?

  24. Shiva Avatar
    Shiva

    typo mistake ,correction** –>

    when i make–> input type = “number” then getting above error

  25. Ajay Malhotra Avatar

    You can use default (countryChange)=”onCountryChange($event)” option.

    Thanks

  26. Bright Agbosu Avatar
    Bright Agbosu

    Hello, thanks for the code you shared.
    I want to control the size but I am unable to.
    Also, how do I set another country as default?

  27. Ajay Malhotra Avatar

    Here is the code:
    add this attribute inside input
    [ng2TelInputOptions]=”{initialCountry: β€˜in’}”

  28. Bright Agbosu Avatar
    Bright Agbosu

    Please the code did not work.

  29. Ajay Malhotra Avatar

    Send me your code on my email: therichposts@gmail.com

  30. Bright Agbosu Avatar
    Bright Agbosu

    I followed the code tutorial and it worked.

  31. Bright Agbosu Avatar
    Bright Agbosu

    The quote mark used is what caused the error so I changed to this:
    [ng2TelInputOptions]=”{initialCountry: ‘us’}”

  32. RN Raagul Avatar
    RN Raagul

    hello, This is working perfectly. but the background is transparent. and other buttons are shown above the drop-down list. is there any way to customize it?

    Thanks

  33. Ajay Malhotra Avatar

    Thank you and yes, you can customize it with css.

  34. Nick Avatar
    Nick

    any idea if this will work without bootstrap ?
    because i tried to implement it in my project and its just an empty input

  35. Ajay Malhotra Avatar
  36. jasmine Avatar
    jasmine

    hello can you help me to fnid the way that make me connect the APi fonction with angular

  37. Ajay Malhotra Avatar

    Yes, you can do that with HTTP Client services.

  38. Junaid Avatar
    Junaid

    How to bind the extension along with the phone number, and then send it to a post request? Only the typed in number is send. But I need to send the number along with the extension

  39. Ajay Malhotra Avatar

    Extension means country code?

  40. karthik Avatar
    karthik

    Hai,
    please help me one this , how can i get separate country flag and separate county code .

  41. Aman Singh Avatar
    Aman Singh

    This is great Ajay, you saved my day. The code works perfectly fine. But there is use case when you are getting the country code from server and setting that using event.setCountry(this.countryID);
    It doesn’t work. Becuase it populate the select boxes before getting the data from API. What do you suggest for that.

  42. Ajay Malhotra Avatar

    Hi Aman and thank you for you comment and I will update you soon.
    Thanks πŸ™‚

  43. JIL SHAH Avatar
    JIL SHAH

    how can set my output like +91 1234567890…country code with phoneno. please reply

  44. Ajay Malhotra Avatar

    you want to set this default value inside input box?

  45. Hashan94 Avatar
    Hashan94

    how can I append the country code with the telephone number. I’m working on aa angular reactive form. I want to pass the entire value on submit.
    ex:
    when I select Sri lankan flag from the dropdown it doesn’t append to the phone number

  46. Roberto Ribeiro Vieira Avatar
    Roberto Ribeiro Vieira

    Thanks for this code.. How can I get the country code to save in DB? and associate to the correct flag when show the number?

  47. Ajay Malhotra Avatar

    I will update you on this.

  48. AYOUB TASNAOUI Avatar
    AYOUB TASNAOUI

    hey thank you for your help it is working,
    i want to know how can excludes some countries or add countries

  49. Ajay Malhotra Avatar

    Try this:
    add this attribute inside input
    [ng2TelInputOptions]=”{initialCountry: β€˜in’}”

  50. Dinesh Avatar
    Dinesh

    Works well, thanks.

  51. Ajay Malhotra Avatar

    You are welcome.

  52. S. VISHNU SHANKAR Avatar
    S. VISHNU SHANKAR

    hi bro, can you please tell me how to change the prefered country ?

  53. Ravikiran Avatar
    Ravikiran

    it is worked for me.
    Thank you boss

  54. Ajay Malhotra Avatar

    You are welcome.

  55. Naveen Avatar
    Naveen

    Hi, I am getting ERROR TypeError: window.intlTelInput is not a function, How to Fix this error.

  56. arul Avatar
    arul

    how can i implement this library in angular material input

  57. Ajay Malhotra Avatar

    Good query and I will update you soon.
    Thanks.

  58. Liga Avatar
    Liga

    How can I get the dial code + phone number on submit?

  59. kavi Avatar