Generally we use regular expression to validate phone number which fails in many cases. For example +91 9677146866 is the correct number which exists, +91 2677146866 is also correct number which doesn't exists. Our regular expression shows both as valid phone numbers. For accurate phone number validation, we will use google-libphonenumber library.
Phone number field with country code
Observe below image. Provide field for country code and number
Validating Phone number with google-libphonenumber
Import Utils from google-libphonenumber
import {PhoneNumberFormat, PhoneNumberUtil} from 'google-libphonenumber';
validatePhoneNumber(phoneNumber) { /* Phone number validation using google-libphonenumber */ let valid = false; try { const phoneUtil = PhoneNumberUtil.getInstance(); valid = phoneUtil.isValidNumber(phoneUtil.parse(phoneNumber)); } catch(e) { valid = false; } if(valid) { // do something } else { // do something } }
Get Valid Phone number in international format
When save numbers in database, you better save them in single international format than many formats. Here is the piece of code which converts normal phone number to international format.
getValidNumber(phoneNumber) { const phoneUtil = PhoneNumberUtil.getInstance(); const parsedNumber = phoneUtil.parse(phoneNumber); return phoneUtil.format(parsedNumber, PhoneNumberFormat.INTERNATIONAL) }
CallingCodes.js
This file contains all country codes, which will be passed to react-select component as options
export default [ {country : 'Afghanistan', value : '93', code : 'AF'}, {country : 'Albania', value : '355', code : 'AL'}, {country : 'Algeria', value : '213', code : 'DZ'}, {country : 'American Samoa', value : '1-684', code : 'AS'}, {country : 'Andorra', value : '376', code : 'AD'} ......... ];
<Select value={this.state.country} onChange={this.onSelect2} placeholder="country code" options={CallingCodes} labelKey="country" valueKey="value" valueRenderer={(country) => country.value}> </Select>
Complete component code
import 'bootstrap/dist/css/bootstrap.min.css'; import 'react-select/dist/react-select.min.css'; import './phone-number.css'; import React from 'react'; import CallingCodes from './CallingCodes'; import {FormControl} from 'react-bootstrap'; import Select from 'react-select'; import {PhoneNumberFormat, PhoneNumberUtil} from 'google-libphonenumber'; export default class App extends React.Component { constructor(props) { super(props); this.state = { country:'', number:'', message:'' } this.onChange = this.onChange.bind(this); this.onSelect2 = this.onSelect2.bind(this); this.validatePhoneNumber = this.validatePhoneNumber.bind(this); } onChange(event) { this.setState({ number:event.target.value }); this.validatePhoneNumber('+'+this.state.country+' '+event.target.value); } onSelect2(cntrObj) { this.setState({ country:cntrObj.value }); this.validatePhoneNumber('+'+cntrObj.value+' '+this.state.number); } validatePhoneNumber(phoneNumber) { /* Phone number validation using google-libphonenumber */ let valid = false; try { const phoneUtil = PhoneNumberUtil.getInstance(); valid = phoneUtil.isValidNumber(phoneUtil.parse(phoneNumber)); } catch(e) { valid = false; } if(valid) { this.setState({ message:'Phone number '+this.getValidNumber(phoneNumber)+' is valid', color:'green' }); } else { this.setState({ message:'Phone number '+phoneNumber+' is not valid', color:'red' }); } } getValidNumber(phoneNumber) { const phoneUtil = PhoneNumberUtil.getInstance(); const parsedNumber = phoneUtil.parse(phoneNumber); return phoneUtil.format(parsedNumber, PhoneNumberFormat.INTERNATIONAL) } render() { return ( <div> <h1>Phone number with country codes using ReactJS</h1> <div className="phone-number" style={{display:'flex'}}> <div className="phone-number--country"> <Select value={this.state.country} onChange={this.onSelect2} placeholder="country code" options={CallingCodes} labelKey="country" valueKey="value" valueRenderer={(country) => country.value}> </Select> </div> <div className="phone-number--number"> <FormControl value={this.state.number} onChange={this.onChange} placeholder="phone number"> </FormControl> </div> </div> <div className="message" style={{color:this.state.color}}> {this.state.message} </div> </div> ) } }
Hey Srinivas, nice post to validate country codes. This is perfect for sites that need offshore accounts to login internationally. Keep up the great work.
ReplyDeleteI have utilized Google on two or three events and discovered unlisted phone numbers that ended up being telemarketers. 0800 number cost
ReplyDeleteUsually these bills are so complex that it is difficult to find these extra lines. This brings me to a major difference in the new telephone systems.free business calls
ReplyDeleteThe primary thing you need to do is to utilize a free inquiry, for example, the White Pagesfree-lookup.net
ReplyDeleteAwesome blog. I would love to see true life prepared to walk, so please share more informative updates. Great work keeps it up. PCNSE exam dumps
ReplyDeleteThis comment has been removed by the author.
ReplyDeletePLC Training in Chennai | PLC Training Institute in Chennai | PLC Training Center in Chennai | PLC SCADA Training in Chennai | PLC SCADA DCS Training in Chennai | Best PLC Training in Chennai | Best PLC Training Institute in Chennai | PLC Training Centre in Chennai | Embedded System Training in Chennai | Embedded Training in Chennai | VLSI Training in Chennai | VLSI Training Institute in Chennai
ReplyDeletePLC Training in Chennai | PLC Training Institute in Chennai | PLC Training Center in Chennai | PLC SCADA Training in Chennai | PLC SCADA DCS Training in Chennai | Best PLC Training in Chennai | Best PLC Training Institute in Chennai | PLC Training Centre in Chennai | PLC SCADA Training in Chennai | Automation Training Institute in Chennai | PLC Training in Kerala | VLSI Training in Chennai | Best VLSI Training in Chennai | VLSI Training Centres in Chennai | VLSI Courses in Chennai | VLSI Training Courses in Chennai | VLSI Training Institute in Chennai | VLSI Training Institutes in Chennai | Best VLSI Training Institute in Chennai
ReplyDeleteEmbedded Training in Chennai | Best Embedded Training in Chennai | Embedded System Training in Chennai | Embedded System Training Institute in Chennai | Best Embedded System Training Institute in Chennai | Embedded Course in Chennai | Embedded System Training Institutes in Chennai | Embedded System Training Center in Chennai | Best Embedded System Training in Chennai | Embedded Systems Training in Chennai | VLSI Training in Chennai | VLSI Training Institute in Chennai
ReplyDeleteThe blog is clear exhibition of the way that, you can bargain with the quality.
ReplyDeleteonline cell phone lookup reviews
This way, the phone can be tracked anywhere and anytime. However, these aforementioned microchips can be quite pricey, but are available online.localiser un portable gratuitement grace a son numero
ReplyDeleteGone were the days that just private and government spies utilize invert telephone investigators. Family units, modern and business foundations, police and crisis reaction groups saw the incredible significance of having these basic telephone numbers query to guarantee that their callers and the circumstances are certifiable.https://www.techpally.com/private-call-announcer-apps/
ReplyDeleteGreat post but I was wondering if you could write a little more on this subject? I’d be very thankful if you could elaborate a little bit further. Thanks in advance! Voyance par telephone
ReplyDeleteFor the uninitiated, a cell phone is a cell phone which can associate with the web and can run and buy applications.oukitel k12
ReplyDeleteExcellent blog, nice work keep it up. Thanks for sharing the required information with a clear update and Good job...!
ReplyDeleteExcel Training in Chennai
Excel classes in Chennai
Tableau Training in Chennai
Oracle Training in Chennai
Job Openings in Chennai
Pega Training in Chennai
Linux Training in Chennai
Spark Training in Chennai
Embedded System Course Chennai
Soft Skills Training in Chennai
Excel Training in T Nagar
iso 27001 certification services
ReplyDeleteiso 27001 certification in delhi
ISO 9001 Certification in Noida
iso 22000 certification in Delhi
iso certification in noida
ReplyDeleteiso certification in delhi
ce certification in delhi
iso 14001 certification in delhi
iso 22000 certification cost
iso consultants in noida
we have provide the best fridge repair service.
ReplyDeleteWashing Machine Repair In Faridabad
LG Washing Machine Repair In Faridabad
Videocon Washing Machine Service Centre In Faridabad
IFB Washing Machine service centre in faridabad
Samsung Washing Machine Repair In Faridabad
Washing Machine Repair in Noida
godrej washing machine repair in noida
whirlpool Washing Machine Repair in Noida
IFB washing Machine Repair in Noida
LG Washing Machine Repair in Noida
we have provide the best ppc service.
ReplyDeleteppc company in gurgaon
website designing company in Gurgaon
PPC company in Noida
seo company in gurgaon
PPC company in Mumbai
PPC company in Chandigarh
Digital Marketing Company
Rice Bags Manufacturers
ReplyDeletePouch Manufacturers
wall putty bag manufacturers
fertilizer bag manufacturers
seed bag manufacturers
gusseted bag manufacturers
bopp laminated bags manufacturer
Lyrics with music
I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business. dan ca
ReplyDeleteI haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. http://locateanumber.com
ReplyDeleteWhen your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. legal entity identifier
ReplyDeleteGreat Article. Thank you for sharing! Really an awesome post for every one.
ReplyDeleteIEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.
Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai
Everyone loves it whenever people come together and share thoughts. Great site, stick with it!
ReplyDeleteTech news
Thanks for sharing Such a Wonderful Blog. Keep update like this
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Course in Bangalore
Software Testing Course in Coimbatore
Software Testing Course in Madurai
Software Testing Institute in Bangalore
Software Testing Training in Bangalore
Software Testing Training Institute in Bangalore
Ethical Hacking Course in Bangalore
Selenium Course in Bangalore
This comment has been removed by the author.
ReplyDeleteA motivating discussion is definitely worth comment. There's no doubt that that you ought to write more about this subject matter, it may not be a taboo matter but generally views people do not talk about these topics. To the next! Best wishes!!
ReplyDeleteThis is a topic that's near to my heart... free Take care! Exactly where are your contact details though?
ReplyDeleteSnapdeal Prize list and Snapdeal prize department. Here you can win the exciting prizes and the special offer just playing a game. For more information visit our website: Snapdeal lucky customer.
ReplyDeleteSnapdeal winner name 2020
Snapdeal lucky draw
Snapdeal lucky customer 2020
Snapdeal winner name list
python course in coimbatore
ReplyDeletepython training in coimbatore
java course in coimbatore
java training in coimbatore
android course in coimbatore
android training in coimbatore
php course in coimbatore
php training in coimbatore
digital marketing course in coimbatore
digital marketing training in coimbatore
software testing course in coimbatore
software testing training in coimbatore
python course in coimbatore
ReplyDeletejava course in coimbatore
python training in coimbatore
java training in coimbatore
php course in coimbatore
php training in coimbatore
android course in coimbatore
android training in coimbatore
datascience course in coimbatore
datascience training in coimbatore
ethical hacking course in coimbatore
ethical hacking training in coimbatore
artificial intelligence course in coimbatore
artificial intelligence training in coimbatore
digital marketing course in coimbatore
digital marketing training in coimbatore
embedded system course in coimbatore
embeddedsystem training in coimbatore
The next time I read a blog, I hope that it does not fail me just as much as this particular technology one. I mean, I know it was my choice to read, nonetheless I actually believed you would probably have something useful to talk about. All I hear is a bunch of whining about something you could fix if you were not too busy looking for attention.
ReplyDeleteI am happy to say that your content is amazing and I learnt new information from your write-up. You have done a great job! Web Designing Course Training in Chennai | Web Designing Course Training in annanagar | Web Designing Course Training in omr | Web Designing Course Training in porur | Web Designing Course Training in tambaram | Web Designing Course Training in velachery
ReplyDeleteBuy Real Passport Online, Buy Fake Passport Online
ReplyDeleteBuy Real Passport Online, Buy Fake Passport Online, Buy Real Documents online, Passport for Sale online, Buy IELTS Certificate online, Drivers license for sale online, Buy Fake ID Cards online, buy documents Online
For more details you can visit our website: https://buyalldocument.com/ OR WhatsApp: +1(443) 351-8162 OR Email us at buyrealfake.documents@yandex.com
THE BEST AGENCY SERVING CLIENTS COUNTRYWIDE
ReplyDeleteLocated in Delhi, Webmarts is a professionally managed digital marketing agency offering a host of demanding services. We offer SEO services, web development, web designing, social media marketing, and more services to clients in different businesses.
Best SEO Company in Delhi
Best seo services in Delhi
Best seo agency in Delhi
Seo service in Delhi
Seo service provider in Delhi
Top rated seo service provider in Delhi
Best seo company in Jankpuri
Best seo company in uttamnagar
Best seo company in Tilak nagar
Very good article you have shared here thank you very much for sharing such kind of blog post.
ReplyDeleteData Science-Alteryx Training Course in Coimbatore | Online Data Science Course in Coimbatore | Data Science Training in Coimbatore | Best Data Science Training Institute | Data Science Course in Coimbatore Online Data Science Training in Coimbatore | Data Science with python Training Course in Coimbatore | Data Science Traning in saravanampatti
Thanks for sharing this information. I really like your post very much. You have really shared an informative and interesting post with people
ReplyDeleteOnline AWS Training Courses in Coimbatore| AWS Training institute Coimbatore| AWS Training and certification in Coimbatore| AWS Training in Saravanampatti | Best AWS Training Courses in Coimbatore| Online Devops Training Center in Coimbatore| Devops Training Institute in Coimbatore| Best Devops Training Center in Coimbatore| Best Institutes for Devops Training in Coimbatore | Online Devops Training and certification in Coimbatore| Devops Training and certification in saravanampatti
Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
ReplyDeleteCatia centre in coimbatore | Catia course in coimbatore | Catia course fees in coimbatore | Catia course training in coimbatore | Best Catia course in coimbatore | Catia course training with placement in coimbatore | Catia online training course in coimbatore | Catia online course in coimbatore | Catia fees structure in coimbatore | Catia jobs in coimbatore | Catia training in coimbatore | Cadd centre in coimbatore | Caadd course in coimbatore | Cadd centre fees structure in coimbatore
https://www.guest-articles.com/education/1z0-1067-oracle-cloud-infrastructure-2019-cloud-operations-associate-02-12-2020
ReplyDeleteERP Gold Software
ReplyDeleteHow ERP Gold works as an inventory management software for Walmart
Why do sellers on Walmart need inventory management software?
Inventory management involves the process of ordering, handling, storing, and processing a company’s inventory. This has done because, without an effective inventory management system, the full potential of the supply chain becomes unattainable.
For more information about ERP Gold inventory management software, please click on the link below.
https://www.erp.gold/walmart-erp-integration-software/
This page contains the necessary information I have a enjoyable site if you feel free you can visit my site Yespornplease
ReplyDeleteThis page contains the necessary information The Samsung Galaxy M52 is going to be published very soon if you feel free you can visit my site Samsung M52 Price
ReplyDeleteThank you for sharing your info. I really appreciate your efforts and I am waiting for your further write ups thank you once again. And know Mimz International Merchandising Company provides services to clients to Make their Vision a Reality and to help companies to improve business performance. merchandising
ReplyDeleteNice informative post. Thanks so much for sharing this awesome details.Retail Electricity Supplier
ReplyDeletehere you can get more information about joining pakistan navy
ReplyDeletehttps://joinpaknavy.site/