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 */ 23goog.provide('i18n.phonenumbers.demo'); 24 25goog.require('goog.dom'); 26goog.require('goog.json'); 27goog.require('goog.proto2.ObjectSerializer'); 28goog.require('goog.string.StringBuffer'); 29goog.require('i18n.phonenumbers.AsYouTypeFormatter'); 30goog.require('i18n.phonenumbers.PhoneNumberFormat'); 31goog.require('i18n.phonenumbers.PhoneNumberType'); 32goog.require('i18n.phonenumbers.PhoneNumberUtil'); 33goog.require('i18n.phonenumbers.PhoneNumberUtil.ValidationResult'); 34goog.require('i18n.phonenumbers.ShortNumberInfo'); 35 36 37/** 38 * @const 39 * @type {!i18n.phonenumbers.PhoneNumberUtil} 40 * @private 41 */ 42var phoneUtil_ = i18n.phonenumbers.PhoneNumberUtil.getInstance(); 43 44function phoneNumberParser() { 45 var $ = goog.dom.getElement; 46 var phoneNumber = $('phoneNumber').value; 47 var regionCode = $('defaultCountry').value.toUpperCase(); 48 var carrierCode = $('carrierCode').value; 49 var output = new goog.string.StringBuffer(); 50 try { 51 var number = phoneUtil_.parseAndKeepRawInput(phoneNumber, regionCode); 52 output.append('****Parsing Result:****\n'); 53 output.append(goog.json.serialize(new goog.proto2.ObjectSerializer( 54 goog.proto2.ObjectSerializer.KeyOption.NAME).serialize(number))); 55 output.append('\n\n****Validation Results:****'); 56 var isPossible = phoneUtil_.isPossibleNumber(number); 57 output.append('\nResult from isPossibleNumber(): '); 58 output.append(isPossible); 59 var validationResult = i18n.phonenumbers.PhoneNumberUtil.ValidationResult; 60 var isPossibleReason = phoneUtil_.isPossibleNumberWithReason(number) 61 var hasRegionCode = regionCode && regionCode != 'ZZ'; 62 if (isPossible) { 63 // Checking as isValid() fails if possible local only. 64 if (isPossibleReason == validationResult.IS_POSSIBLE_LOCAL_ONLY) { 65 output.append('\nResult from isPossibleNumberWithReason(): '); 66 output.append('IS_POSSIBLE_LOCAL_ONLY'); 67 output.append( 68 '\nNumber is considered invalid as it is ' + 69 'not a possible national number.'); 70 } else { 71 var isNumberValid = phoneUtil_.isValidNumber(number); 72 output.append('\nResult from isValidNumber(): '); 73 output.append(isNumberValid); 74 if (isNumberValid && hasRegionCode) { 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 output.append(getNumberTypeString(number)); 82 } 83 } else { 84 output.append('\nResult from isPossibleNumberWithReason(): '); 85 switch (isPossibleReason) { 86 case validationResult.INVALID_COUNTRY_CODE: 87 output.append('INVALID_COUNTRY_CODE'); 88 break; 89 case validationResult.TOO_SHORT: 90 output.append('TOO_SHORT'); 91 break; 92 case validationResult.TOO_LONG: 93 output.append('TOO_LONG'); 94 break; 95 case validationResult.INVALID_LENGTH: 96 output.append('INVALID_LENGTH'); 97 break; 98 } 99 // IS_POSSIBLE shouldn't happen, since we only call this if _not_ 100 // possible. 101 output.append( 102 '\nNote: Numbers that are not possible have type UNKNOWN,' + 103 ' an unknown region, and are considered invalid.'); 104 } 105 if (!isNumberValid) { 106 var shortInfo = i18n.phonenumbers.ShortNumberInfo.getInstance(); 107 output.append('\n\n****ShortNumberInfo Results:****'); 108 output.append('\nResult from isPossibleShortNumber: '); 109 output.append(shortInfo.isPossibleShortNumber(number)); 110 output.append('\nResult from isValidShortNumber: '); 111 output.append(shortInfo.isValidShortNumber(number)); 112 if (hasRegionCode) { 113 output.append('\nResult from isPossibleShortNumberForRegion: '); 114 output.append( 115 shortInfo.isPossibleShortNumberForRegion(number, regionCode)); 116 output.append('\nResult from isValidShortNumberForRegion: '); 117 output.append( 118 shortInfo.isValidShortNumberForRegion(number, regionCode)); 119 } 120 } 121 122 var PNF = i18n.phonenumbers.PhoneNumberFormat; 123 output.append('\n\n****Formatting Results:**** '); 124 output.append('\nE164 format: '); 125 output.append( 126 isNumberValid ? phoneUtil_.format(number, PNF.E164) : 'invalid'); 127 output.append('\nOriginal format: '); 128 output.append(phoneUtil_.formatInOriginalFormat(number, regionCode)); 129 output.append('\nNational format: '); 130 output.append(phoneUtil_.format(number, PNF.NATIONAL)); 131 output.append('\nInternational format: '); 132 output.append( 133 isNumberValid ? phoneUtil_.format(number, PNF.INTERNATIONAL) : 134 'invalid'); 135 output.append('\nOut-of-country format from US: '); 136 output.append( 137 isNumberValid ? 138 phoneUtil_.formatOutOfCountryCallingNumber(number, 'US') : 139 'invalid'); 140 output.append('\nOut-of-country format from Switzerland: '); 141 output.append( 142 isNumberValid ? 143 phoneUtil_.formatOutOfCountryCallingNumber(number, 'CH') : 144 'invalid'); 145 if (carrierCode.length > 0) { 146 output.append('\nNational format with carrier code: '); 147 output.append( 148 phoneUtil_.formatNationalNumberWithCarrierCode(number, carrierCode)); 149 } 150 output.append('\n\n****AsYouTypeFormatter Results****'); 151 var formatter = new i18n.phonenumbers.AsYouTypeFormatter(regionCode); 152 var phoneNumberLength = phoneNumber.length; 153 for (var i = 0; i < phoneNumberLength; ++i) { 154 var inputChar = phoneNumber.charAt(i); 155 output.append('\nChar entered: '); 156 output.append(inputChar); 157 output.append(' Output: '); 158 output.append(formatter.inputDigit(inputChar)); 159 } 160 } catch (e) { 161 output.append('\n' + e.toString()); 162 } 163 $('output').value = output.toString(); 164 return false; 165} 166 167function getNumberTypeString(number) { 168 switch (phoneUtil_.getNumberType(number)) { 169 case i18n.phonenumbers.PhoneNumberType.FIXED_LINE: 170 return 'FIXED_LINE'; 171 case i18n.phonenumbers.PhoneNumberType.MOBILE: 172 return 'MOBILE' 173 case i18n.phonenumbers.PhoneNumberType.FIXED_LINE_OR_MOBILE: 174 return 'FIXED_LINE_OR_MOBILE'; 175 case i18n.phonenumbers.PhoneNumberType.TOLL_FREE: 176 return 'TOLL_FREE'; 177 case i18n.phonenumbers.PhoneNumberType.PREMIUM_RATE: 178 return 'PREMIUM_RATE'; 179 case i18n.phonenumbers.PhoneNumberType.SHARED_COST: 180 return 'SHARED_COST'; 181 case i18n.phonenumbers.PhoneNumberType.VOIP: 182 return 'VOIP'; 183 case i18n.phonenumbers.PhoneNumberType.PERSONAL_NUMBER: 184 return 'PERSONAL_NUMBER'; 185 case i18n.phonenumbers.PhoneNumberType.PAGER: 186 return 'PAGER'; 187 case i18n.phonenumbers.PhoneNumberType.UAN: 188 return 'UAN'; 189 case i18n.phonenumbers.PhoneNumberType.UNKNOWN: 190 return 'UNKNOWN'; 191 } 192} 193 194goog.exportSymbol('phoneNumberParser', phoneNumberParser); 195