2

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 i18nis not defined or PhoneNumberUtilis 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!

4

2 回答 2

5

我让它工作了!您需要使用闭包编译器http://closure-compiler.appspot.com/使用您的代码编译 libphonenumber 库:

// ==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;
};
于 2013-08-06T14:45:06.717 回答
0

您可以使用 API 服务评估最新的电话号码详细信息:

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
 }

完整指南:http ://blog.ones-app.com/ones-phone-number-api/

于 2016-02-03T13:04:30.193 回答