Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return (phoneControl: AbstractControl): {[key: string]: boolean} => {
if (!subscribe) {
subscribe = true;
countryControl.valueChanges.subscribe(() => {
phoneControl.updateValueAndValidity();
});
}
if(phoneControl.value !== ""){
try{
const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
let phoneNumber = "" + phoneControl.value + "",
region = countryControl.value.iso,
number = phoneUtil.parse(phoneNumber, region),
isValidNumber = phoneUtil.isValidNumber(number);
if(isValidNumber){
return null;
}
}catch(e){
// console.log(e);
return {
validCountryPhone: true
};
}
return {
public isValidMobile(_phonenumber, country): any {
try {
const phoneUtil = phonenumbers.PhoneNumberUtil.getInstance();
const phoneNumber = phoneUtil.parse(_phonenumber, country);
const isValid = phoneUtil.isValidNumber(phoneNumber);
// In some regions (e.g. the USA), it is impossible to distinguish between
// fixed-line and mobile numbers by looking at the phone number itself
const numberType = phoneUtil.getNumberType(phoneNumber);
const isMobile = numberType === phonenumbers.PhoneNumberType.MOBILE ||
numberType === phonenumbers.PhoneNumberType.FIXED_LINE_OR_MOBILE;
if (isValid && isMobile) { return true; }
} catch (e) {
console.error(e);
}
return false;
}
}
public isValidMobile(_phonenumber, country): any {
try {
const phoneUtil = phonenumbers.PhoneNumberUtil.getInstance();
const phoneNumber = phoneUtil.parse(_phonenumber, country);
const isValid = phoneUtil.isValidNumber(phoneNumber);
// In some regions (e.g. the USA), it is impossible to distinguish between
// fixed-line and mobile numbers by looking at the phone number itself
const numberType = phoneUtil.getNumberType(phoneNumber);
const isMobile = numberType === phonenumbers.PhoneNumberType.MOBILE ||
numberType === phonenumbers.PhoneNumberType.FIXED_LINE_OR_MOBILE;
if (isValid && isMobile) { return true; }
} catch (e) {
console.error(e);
}
return false;
}
}
var numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);
return phoneUtil.isPossibleNumberWithReason(numberObj);
} catch (e) {
//console.log(e);
// here I convert thrown errors into ValidationResult enums (if possible)
if (e == libphonenumber.Error.INVALID_COUNTRY_CODE) {
return libphonenumber.PhoneNumberUtil.ValidationResult.INVALID_COUNTRY_CODE;
}
if (e == libphonenumber.Error.NOT_A_NUMBER) {
return 4;
}
if (e == libphonenumber.Error.TOO_SHORT_AFTER_IDD || e == libphonenumber.Error.TOO_SHORT_NSN) {
return libphonenumber.PhoneNumberUtil.ValidationResult.TOO_SHORT;
}
if (e == libphonenumber.Error.TOO_LONG) {
return libphonenumber.PhoneNumberUtil.ValidationResult.TOO_LONG;
}
// broken
return -99;
}
}
function getValidationError(number, countryCode) {
try {
var phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
var numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);
return phoneUtil.isPossibleNumberWithReason(numberObj);
} catch (e) {
//console.log(e);
// here I convert thrown errors into ValidationResult enums (if possible)
if (e == libphonenumber.Error.INVALID_COUNTRY_CODE) {
return libphonenumber.PhoneNumberUtil.ValidationResult.INVALID_COUNTRY_CODE;
}
if (e == libphonenumber.Error.NOT_A_NUMBER) {
return 4;
}
if (e == libphonenumber.Error.TOO_SHORT_AFTER_IDD || e == libphonenumber.Error.TOO_SHORT_NSN) {
return libphonenumber.PhoneNumberUtil.ValidationResult.TOO_SHORT;
}
if (e == libphonenumber.Error.TOO_LONG) {
return libphonenumber.PhoneNumberUtil.ValidationResult.TOO_LONG;
}
// broken
return -99;
}
}
function getValidationError(number, countryCode) {
try {
var phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
var numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);
return phoneUtil.isPossibleNumberWithReason(numberObj);
} catch (e) {
//console.log(e);
// here I convert thrown errors into ValidationResult enums (if possible)
if (e == libphonenumber.Error.INVALID_COUNTRY_CODE) {
return libphonenumber.PhoneNumberUtil.ValidationResult.INVALID_COUNTRY_CODE;
}
if (e == libphonenumber.Error.NOT_A_NUMBER) {
return 4;
}
if (e == libphonenumber.Error.TOO_SHORT_AFTER_IDD || e == libphonenumber.Error.TOO_SHORT_NSN) {
return libphonenumber.PhoneNumberUtil.ValidationResult.TOO_SHORT;
}
if (e == libphonenumber.Error.TOO_LONG) {
return libphonenumber.PhoneNumberUtil.ValidationResult.TOO_LONG;
}
// broken
return -99;
}
}
function getValidationError(number, countryCode) {
try {
var phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
var numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);
return phoneUtil.isPossibleNumberWithReason(numberObj);
} catch (e) {
//console.log(e);
// here I convert thrown errors into ValidationResult enums (if possible)
if (e == libphonenumber.Error.INVALID_COUNTRY_CODE) {
return libphonenumber.PhoneNumberUtil.ValidationResult.INVALID_COUNTRY_CODE;
}
if (e == libphonenumber.Error.NOT_A_NUMBER) {
return 4;
}
if (e == libphonenumber.Error.TOO_SHORT_AFTER_IDD || e == libphonenumber.Error.TOO_SHORT_NSN) {
return libphonenumber.PhoneNumberUtil.ValidationResult.TOO_SHORT;
}
if (e == libphonenumber.Error.TOO_LONG) {
return libphonenumber.PhoneNumberUtil.ValidationResult.TOO_LONG;
}
// broken
return -99;
}
}
function formatNumber(val, countryCode, addSuffix, allowExtension, isAllowedKey) {
try {
var clean = val.replace(/\D/g, ""),
// NOTE: we use AsYouTypeFormatter because the default format function can't handle incomplete numbers e.g. "+17024" formats to "+1 7024" as opposed to "+1 702-4"
formatter = new libphonenumber.AsYouTypeFormatter(countryCode),
// if clean is empty, we still need this to be a string otherwise we get errors later
result = "",
next,
extSuffix = " ext. ";
if (val.substr(0, 1) == "+") {
clean = "+" + clean;
}
for (var i = 0; i < clean.length; i++) {
// TODO: improve this so don't just pump in every digit every time - we should just cache this formatter object, and just call inputDigit once each time the user enters a new digit
next = formatter.inputDigit(clean.charAt(i));
// if adding this char didn't change the length, or made it smaller (and there's no longer any spaces): that means that formatting failed which means the number was no longer a potentially valid number, so if we're allowing extensions: assume the rest is the ext
if (allowExtension && result && next.length <= result.length && next.indexOf(" ") == -1) {
// set flag for extension
next = -1;
if (phoneNumberSequenceCount === 1) {
this.$speech.t('component-GetPhoneNumber.sequence-question');
this.$reprompt.t('component-GetPhoneNumber.sequence-reprompt');
return this.ask(this.$speech, this.$reprompt);
}
else if (phoneNumberSequenceCount === 2) {
this.$speech.t('component-GetPhoneNumber.sequence-last-digits-question');
this.$reprompt.t('component-GetPhoneNumber.sequence-last-digits-reprompt');
return this.ask(this.$speech, this.$reprompt);
}
else {
// validate phone number
const region = this.$request!.getLocale().split('-')[1]; // e.g. "en-US" -> "US"
let phoneNumber: PhoneNumber = new PhoneNumber();
try {
phoneNumber = phoneNumberUtilInstance.parse(this.$session.$data.COMPONENT_GetPhoneNumber.phoneNumberRaw, region);
} catch (error) {
return invalidNumber(this);
}
if (!phoneNumberUtilInstance.isValidNumber(phoneNumber)) {
return invalidNumber(this);
}
const formattedPhoneNumber = formatPhoneNumber(phoneNumber);
this.$components.GetPhoneNumber.data.phoneNumber = formattedPhoneNumber;
return sendComponentResponse(this, 'SUCCESSFUL');
}
// Copyright The Linux Foundation and each contributor to CommunityBridge.
// SPDX-License-Identifier: MIT
import { FormControl } from '@angular/forms';
declare var require: any;
var phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
export class PhoneNumberValidator {
static isValid(control: FormControl): any {
let number = control.value;
let phoneProto;
if(number == null || number == '') {
return null;
}
let countryCode = 'US';
if(number.charAt(0)==='+') {
countryCode = 'ZZ';
}