问题描述
我绝对不是JavaScript开发新手.我需要的是验证手机号码以在一段时间后启用短信.
I am absolutely new to javascript development. What I need is validating a mobile number to enable sms after some time.
我发现的是Googles libphonenumber
: https://code.google.com/p/libphonenumber/
What I've found is googles libphonenumber
: https://code.google.com/p/libphonenumber/
我以为我在源中继中有一个已编译的javascript文件(closure-compiler),我可以简单地执行以下操作:
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;
};
但是我得到一个ReferenceError
-未定义i18n
或未定义PhoneNumberUtil
.如何使用已编译的库?我是否缺少某些东西,或者需要使用该库编译我的代码?
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!
推荐答案
我成功了!您需要使用闭包编译器 http://closure-compiler.appspot.com/与代码一起编译libphonenumber库. :
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;
};
这篇关于libphonenumber JavaScript-验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!