libphonenumber javascript - 验证

时间:2013-07-29 15:32:29

标签: javascript validation libphonenumber

我是javascript开发的新手。我需要的是验证手机号码,以便在一段时间后启用短信。

我发现的是谷歌libphonenumberhttps://code.google.com/p/libphonenumber/

我认为在源干线中有一个已编译的javascript文件(closure-compiler),我可以简单地执行以下操作:

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;
};

但我收到ReferenceError - 未定义i18n或未定义PhoneNumberUtil。 如何使用已编译的库? 我错过了什么或者我需要用库编译我的代码吗?

我希望得到任何帮助,谢谢!

2 个答案:

答案 0 :(得分: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;
};

答案 1 :(得分: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/