1/** 2 * @license 3 * Copyright (C) 2010 The Libphonenumber Authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18/** 19 * @fileoverview Phone Number Parser Demo. 20 * 21 * @author Nikolaos Trogkanis 22 */ 23 24goog.require('goog.dom'); 25goog.require('goog.json'); 26goog.require('goog.proto2.ObjectSerializer'); 27goog.require('goog.string.StringBuffer'); 28goog.require('i18n.phonenumbers.AsYouTypeFormatter'); 29goog.require('i18n.phonenumbers.PhoneNumberFormat'); 30goog.require('i18n.phonenumbers.PhoneNumberType'); 31goog.require('i18n.phonenumbers.PhoneNumberUtil'); 32goog.require('i18n.phonenumbers.PhoneNumberUtil.ValidationResult'); 33goog.require('i18n.phonenumbers.ShortNumberInfo'); 34 35 36function phoneNumberParser() { 37 var $ = goog.dom.getElement; 38 var phoneNumber = $('phoneNumber').value; 39 var regionCode = $('defaultCountry').value.toUpperCase(); 40 var carrierCode = $('carrierCode').value; 41 var output = new goog.string.StringBuffer(); 42 try { 43 var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance(); 44 var number = phoneUtil.parseAndKeepRawInput(phoneNumber, regionCode); 45 output.append('****Parsing Result:****\n'); 46 output.append(goog.json.serialize(new goog.proto2.ObjectSerializer( 47 goog.proto2.ObjectSerializer.KeyOption.NAME).serialize(number))); 48 output.append('\n\n****Validation Results:****'); 49 var isPossible = phoneUtil.isPossibleNumber(number); 50 output.append('\nResult from isPossibleNumber(): '); 51 output.append(isPossible); 52 if (!isPossible) { 53 output.append('\nResult from isPossibleNumberWithReason(): '); 54 var PNV = i18n.phonenumbers.PhoneNumberUtil.ValidationResult; 55 switch (phoneUtil.isPossibleNumberWithReason(number)) { 56 case PNV.INVALID_COUNTRY_CODE: 57 output.append('INVALID_COUNTRY_CODE'); 58 break; 59 case PNV.TOO_SHORT: 60 output.append('TOO_SHORT'); 61 break; 62 case PNV.TOO_LONG: 63 output.append('TOO_LONG'); 64 break; 65 } 66 // IS_POSSIBLE shouldn't happen, since we only call this if _not_ 67 // possible. 68 output.append('\nNote: numbers that are not possible have type ' + 69 'UNKNOWN, an unknown region, and are considered invalid.'); 70 } else { 71 var isNumberValid = phoneUtil.isValidNumber(number); 72 output.append('\nResult from isValidNumber(): '); 73 output.append(isNumberValid); 74 if (isNumberValid && regionCode && regionCode != 'ZZ') { 75 output.append('\nResult from isValidNumberForRegion(): '); 76 output.append(phoneUtil.isValidNumberForRegion(number, regionCode)); 77 } 78 output.append('\nPhone Number region: '); 79 output.append(phoneUtil.getRegionCodeForNumber(number)); 80 output.append('\nResult from getNumberType(): '); 81 var PNT = i18n.phonenumbers.PhoneNumberType; 82 switch (phoneUtil.getNumberType(number)) { 83 case PNT.FIXED_LINE: 84 output.append('FIXED_LINE'); 85 break; 86 case PNT.MOBILE: 87 output.append('MOBILE'); 88 break; 89 case PNT.FIXED_LINE_OR_MOBILE: 90 output.append('FIXED_LINE_OR_MOBILE'); 91 break; 92 case PNT.TOLL_FREE: 93 output.append('TOLL_FREE'); 94 break; 95 case PNT.PREMIUM_RATE: 96 output.append('PREMIUM_RATE'); 97 break; 98 case PNT.SHARED_COST: 99 output.append('SHARED_COST'); 100 break; 101 case PNT.VOIP: 102 output.append('VOIP'); 103 break; 104 case PNT.PERSONAL_NUMBER: 105 output.append('PERSONAL_NUMBER'); 106 break; 107 case PNT.PAGER: 108 output.append('PAGER'); 109 break; 110 case PNT.UAN: 111 output.append('UAN'); 112 break; 113 case PNT.UNKNOWN: 114 output.append('UNKNOWN'); 115 break; 116 } 117 } 118 var shortInfo = i18n.phonenumbers.ShortNumberInfo.getInstance(); 119 output.append('\n\n****ShortNumberInfo Results:****'); 120 output.append('\nResult from isPossibleShortNumber: '); 121 output.append(shortInfo.isPossibleShortNumber(number)); 122 output.append('\nResult from isValidShortNumber: '); 123 output.append(shortInfo.isValidShortNumber(number)); 124 output.append('\nResult from isPossibleShortNumberForRegion: '); 125 output.append(shortInfo.isPossibleShortNumberForRegion(number, regionCode)); 126 output.append('\nResult from isValidShortNumberForRegion: '); 127 output.append(shortInfo.isValidShortNumberForRegion(number, regionCode)); 128 129 var PNF = i18n.phonenumbers.PhoneNumberFormat; 130 output.append('\n\n****Formatting Results:**** '); 131 output.append('\nE164 format: '); 132 output.append(isNumberValid ? 133 phoneUtil.format(number, PNF.E164) : 134 'invalid'); 135 output.append('\nOriginal format: '); 136 output.append(phoneUtil.formatInOriginalFormat(number, regionCode)); 137 output.append('\nNational format: '); 138 output.append(phoneUtil.format(number, PNF.NATIONAL)); 139 output.append('\nInternational format: '); 140 output.append(isNumberValid ? 141 phoneUtil.format(number, PNF.INTERNATIONAL) : 142 'invalid'); 143 output.append('\nOut-of-country format from US: '); 144 output.append(isNumberValid ? 145 phoneUtil.formatOutOfCountryCallingNumber(number, 'US') : 146 'invalid'); 147 output.append('\nOut-of-country format from Switzerland: '); 148 output.append(isNumberValid ? 149 phoneUtil.formatOutOfCountryCallingNumber(number, 'CH') : 150 'invalid'); 151 if (carrierCode.length > 0) { 152 output.append('\nNational format with carrier code: '); 153 output.append(phoneUtil.formatNationalNumberWithCarrierCode(number, 154 carrierCode)); 155 } 156 output.append('\n\n****AsYouTypeFormatter Results****'); 157 var formatter = new i18n.phonenumbers.AsYouTypeFormatter(regionCode); 158 var phoneNumberLength = phoneNumber.length; 159 for (var i = 0; i < phoneNumberLength; ++i) { 160 var inputChar = phoneNumber.charAt(i); 161 output.append('\nChar entered: '); 162 output.append(inputChar); 163 output.append(' Output: '); 164 output.append(formatter.inputDigit(inputChar)); 165 } 166 } catch (e) { 167 output.append('\n' + e.toString()); 168 } 169 $('output').value = output.toString(); 170 return false; 171} 172 173goog.exportSymbol('phoneNumberParser', phoneNumberParser); 174