Categories

Wednesday, December 25, 2024
#919814419350 therichposts@gmail.com
Bootstrap 4Bootstrap 4.5Reactjs

Reactjs International Telephone Input

Reactjs International Telephone Input

Hello friends, welcome back to my blog. Today in this blog post, I am going to show you, Reactjs International Telephone Input.

In this post, we will do below steps:

  1. Add country code and courting flag to telephone input in react js.
  2. Set and get the telephone input.
  3. Countries Check.

Reactjs International Telephone Input
Reactjs International Telephone Input
Reactjs International Telephone Input with few countries
Reactjs International Telephone Input with few countries

For reactjs new comers, please check the below link:

Reactjs Basic Tutorials


Friends now I proceed onwards and here is the working code snippet and please use this carefully to avoid the mistakes:

1. Firstly, we need fresh reactjs setup and for that, we need to run below commands into out terminal and also we should have latest node version installed on our system:

npx create-react-app reacttemplate

cd reacttemplate

npm start

2. Now we need to run below commands into our project terminal to get bootstrap and related modules into our reactjs application:

npm install bootstrap --save

npm install react-phone-input-2 --save

npm start //For start project again

3. Finally for the main output, we need to add below code into our reacttemplate/src/App.js file or if you have fresh setup then you can replace reacttemplate/src/App.js file code with below code:

import React from "react";

import './App.css'
import 'bootstrap/dist/css/bootstrap.min.css';

//Importing input tepp modules
import PhoneInput from 'react-phone-input-2'
import 'react-phone-input-2/lib/style.css'


class App extends React.Component {
 //setting default phone number
 constructor(props) {
  super(props);
  this.state = {phone: "13338965474"};
}

 
  render() {
    return (
      <div className="main_container">
     
        <div class="container">
        
          <p>Current Phone Number is : {this.state.phone}</p>
          <br></br>
        <form>
        <div class="form-group">
        <PhoneInput
          country={'us'}
          value={this.state.phone}
          onChange={phone => this.setState({ phone })}
        />
                </div>
               

               <div className="form-group">
                 <label>Only Few Countries Available</label>
               <PhoneInput
               country={'gr'}
                onlyCountries={['gr', 'fr', 'us']}
                />
               </div>
                
        </form>
        </div>
            
       

      </div>
    );
  }
}
export default App;

Now we are done friends. If you have any kind of query or suggestion or any requirement then feel free to comment below.

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

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

2 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.