Validate Mobile number with their country codes
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 —
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 —
Thanks, please comment !:)