libphonenumber javascript - validating

2019-07-18 11:00发布

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!

2条回答
女痞
2楼-- · 2019-07-18 11:36

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;
};
查看更多
叛逆
3楼-- · 2019-07-18 11:47

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/

查看更多
登录 后发表回答