Validate Mobile number with their country codes

Shivam Gupta
1 min readOct 18, 2018

--

To validate mobile number along with country code using plain javascript or in angular version 1 , you can also do it any js framework using npm. I will share links at the end of this article.

i am using googles libphonenumber library.

add in html —
<script src=”https://unpkg.com/libphonenumber-js/bundle/libphonenumber-js.min.js"></script>

JS —
// number will be with country code
function isValidNumber(number) {
return new libphonenumber.parsePhoneNumber(number).isValid()
}

isValidNumber(“+917111111000”) => true (valid for india)

isValidNumber(“+9172425525100”) => false

See working example —

Javascript — Validate Mobile number using javascript

Example 2 (another approach)

isValidMobile() {
var phoneNumber = new libphonenumber.parsePhoneNumber(‘999999999’, ‘IN’)
phoneNumber.country === ‘RU’
phoneNumber.number === ‘+78005553535’
phoneNumber.isValid() === true

}

You can use this library by npm with any framework Angular2+, React.js find complete reference —

https://github.com/catamphetamine/libphonenumber-js

https://www.npmjs.com/package/libphonenumber-js

Thanks, please comment !:)

--

--

Shivam Gupta
Shivam Gupta

Written by Shivam Gupta

Full Stack Engineer (Web/App) working on different JS Technologies & frameworks— Angular, Node, Typescript, Ionic, Firebase, AWS, ElK...Love to write cool stuff

No responses yet