gpt4 book ai didi

javascript - libphonenumber javascript - 验证

转载 作者:行者123 更新时间:2023-11-29 17:15:56 24 4
gpt4 key购买 nike

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

我发现的是 googles libphonenumber:https://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 未定义。如何使用已编译的库?我是否遗漏了什么或者我是否需要使用该库编译我的代码?

如果有任何帮助,我将不胜感激,在此先感谢!

最佳答案

我成功了!您需要使用闭包编译器将 libphonenumber 库与您的代码一起编译 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;
};

关于javascript - libphonenumber javascript - 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17928116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com