2013-07-29 72 views
2

我絕對是JavaScript開發的新手。我需要的是驗證一個手機號碼,以便在一段時間後啓用短信。libphonenumber javascript - validating

什麼我發現是谷歌libphonenumberhttps://code.google.com/p/libphonenumber/

有一個在源極主編譯JavaScript文件(封編譯)一個我想,我可以簡單地做到以下幾點:

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未定義。 如何使用編譯的庫? 我錯過了什麼,或者是否需要編譯我的代碼庫?

我將不勝感激任何幫助,提前致謝!

+0

供參考:這個插件不支持一些國家,http://phpinterviewquestions.co.in/blog/ionic/googles-libphonenumber-library-supported-country-list –

回答

4

我明白了! 您需要使用封閉編譯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

我使用C#和我下載的lib phonenumber from nuget,但我不斷得到'getNumberType'不是'PhoneNumberUtil'的成員有什麼想法? – 2014-05-15 15:00:14

+0

nuget的版本是一個非官方的端口,目前看起來沒有太好的維護。我有幸運氣直接從Google下載Java .jar並使用IKVM將其轉換爲.NET .dll - > http://www.ikvm.net/ – dana

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/

相關問題