I am absolutely new to javascript development. What I need is validating a mobile number to enable sms after some time.
What I've found is googles libphonenumber
: https://code.google.com/p/libphonenumber/
There is a compiled javascript file (closure-compiler) in the source trunk an I thought, I could simply do the following:
function buildAndValidatePhone(phoneNumber, countryCode) {
var strIntlNumber = "invalid";
try {
var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
var regionCode = phoneUtil.getRegionCodeForCountryCode(countryCode);
var number = phoneUtil.parseAndKeepRawInput(phoneNumber, regionCode);
if (phoneUtil.isValidNumber(number)) {
var PNT = i18n.phonenumbers.PhoneNumberType;
var numberType = phoneUtil.getNumberType(number);
if (numberType == PNT.MOBILE) {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
strIntlNumber = phoneUtil.format(number, PNF.E164);
strIntlNumber = strIntlNumber.replace('+','');
}
}
}
catch(ex) {
console.log(ex);
}
return strIntlNumber;
};
But I am getting a ReferenceError
- either i18n
is not defined or PhoneNumberUtil
is not defined.
How do I work with the compiled library?
Am I missing something or do I need to compile my code with the library?
I would appreciate any help, thanks in advance!
I got it working!
You need to compile the libphonenumber library with your code using the closure compiler http://closure-compiler.appspot.com/:
// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// @use_closure_library true
// @code_url http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/phonemetadata.pb.js
// @code_url http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/phonenumber.pb.js
// @code_url http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/metadatalite.js
// @code_url http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/phonenumberutil.js
// @code_url http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/asyoutypeformatter.js
// ==/ClosureCompiler==
function buildAndValidatePhone(phoneNumber, countryCode) {
var strIntlNumber = "invalid";
try {
var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
var regionCode = phoneUtil.getRegionCodeForCountryCode(countryCode);
var number = phoneUtil.parseAndKeepRawInput(phoneNumber, regionCode);
if (phoneUtil.isValidNumber(number)) {
var PNT = i18n.phonenumbers.PhoneNumberType;
var numberType = phoneUtil.getNumberType(number);
if (numberType == PNT.MOBILE) {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
strIntlNumber = phoneUtil.format(number, PNF.E164);
strIntlNumber = strIntlNumber.replace('+','');
}
}
}
catch(ex) {
console.log(ex);
}
return strIntlNumber;
};
You can use API Service to evaluate latest Phone Number details:
curl http://phonenumber.ones-app.com/details?number=004532160398
{
"phonenumber": "4532160398",
"code": "45",
"nsn": "32160398",
"typeid": "FIXED_LINE_OR_MOBILE",
"operator": "Telenor",
"iso3": "DNK",
"iso2": "DK",
"country": "Denmark",
"timezone": ["Europe/Copenhagen"]("Europe/Copenhagen"],),
"is_validnumber": true,
"is_validnumberforRegion": true,
"is_possiblenumber": true
}
For Complete Guide: http://blog.ones-app.com/ones-phone-number-api/