UNPKG

ngx-phone-validators

Version:

An implementation of angular validators for Angular 2 and higher

484 lines (474 loc) 14.4 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('google-libphonenumber'), require('@angular/core'), require('@angular/forms')) : typeof define === 'function' && define.amd ? define('ngx-phone-validators', ['exports', 'google-libphonenumber', '@angular/core', '@angular/forms'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['ngx-phone-validators'] = {}, global.googleLibphonenumber, global.ng.core, global.ng.forms)); }(this, (function (exports, googleLibphonenumber, core, forms) { 'use strict'; var Util = /** @class */ (function () { function Util() { } Util.isNotPresent = function (control) { var value = control.value; if (value === undefined || value === null) { return true; } return value !== "" ? false : true; }; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types,@typescript-eslint/no-explicit-any Util.addError = function (control, errorId, value) { var _a; if (!control.errors) { control.setErrors((_a = {}, _a[errorId] = value, _a)); } else if (!control.hasError(errorId)) { control.errors[errorId] = value; } }; Util.removeError = function (control, errorId) { if (control.errors && control.hasError(errorId)) { delete control.errors[errorId]; } }; return Util; }()); var regionsCode = { // Region code for global networks (e.g. +800 numbers). UN001: "001", AD: "AD", AE: "AE", AF: "AF", AG: "AG", AI: "AI", AL: "AL", AM: "AM", AN: "AN", AO: "AO", AQ: "AQ", AR: "AR", AS: "AS", AT: "AT", AU: "AU", AW: "AW", AX: "AX", AZ: "AZ", BA: "BA", BB: "BB", BD: "BD", BE: "BE", BF: "BF", BG: "BG", BH: "BH", BI: "BI", BJ: "BJ", BL: "BL", BM: "BM", BN: "BN", BO: "BO", BR: "BR", BS: "BS", BT: "BT", BV: "BV", BW: "BW", BY: "BY", BZ: "BZ", CA: "CA", CC: "CC", CD: "CD", CF: "CF", CG: "CG", CH: "CH", CI: "CI", CK: "CK", CL: "CL", CM: "CM", CN: "CN", CO: "CO", CR: "CR", CU: "CU", CV: "CV", CX: "CX", CY: "CY", CZ: "CZ", DE: "DE", DJ: "DJ", DK: "DK", DM: "DM", DO: "DO", DZ: "DZ", EC: "EC", EE: "EE", EG: "EG", EH: "EH", ER: "ER", ES: "ES", ET: "ET", FI: "FI", FJ: "FJ", FK: "FK", FM: "FM", FO: "FO", FR: "FR", GA: "GA", GB: "GB", GD: "GD", GE: "GE", GF: "GF", GG: "GG", GH: "GH", GI: "GI", GL: "GL", GM: "GM", GN: "GN", GP: "GP", GQ: "GQ", GR: "GR", GS: "GS", GT: "GT", GU: "GU", GW: "GW", GY: "GY", HK: "HK", HM: "HM", HN: "HN", HR: "HR", HT: "HT", HU: "HU", ID: "ID", IE: "IE", IL: "IL", IM: "IM", IN: "IN", IO: "IO", IQ: "IQ", IR: "IR", IS: "IS", IT: "IT", JE: "JE", JM: "JM", JO: "JO", JP: "JP", KE: "KE", KG: "KG", KH: "KH", KI: "KI", KM: "KM", KN: "KN", KP: "KP", KR: "KR", KW: "KW", KY: "KY", KZ: "KZ", LA: "LA", LB: "LB", LC: "LC", LI: "LI", LK: "LK", LR: "LR", LS: "LS", LT: "LT", LU: "LU", LV: "LV", LY: "LY", MA: "MA", MC: "MC", MD: "MD", ME: "ME", MF: "MF", MG: "MG", MH: "MH", MK: "MK", ML: "ML", MM: "MM", MN: "MN", MO: "MO", MP: "MP", MQ: "MQ", MR: "MR", MS: "MS", MT: "MT", MU: "MU", MV: "MV", MW: "MW", MX: "MX", MY: "MY", MZ: "MZ", NA: "NA", NC: "NC", NE: "NE", NF: "NF", NG: "NG", NI: "NI", NL: "NL", NO: "NO", NP: "NP", NR: "NR", NU: "NU", NZ: "NZ", OM: "OM", PA: "PA", PE: "PE", PF: "PF", PG: "PG", PH: "PH", PK: "PK", PL: "PL", PM: "PM", PN: "PN", PR: "PR", PS: "PS", PT: "PT", PW: "PW", PY: "PY", QA: "QA", RE: "RE", RO: "RO", RS: "RS", RU: "RU", RW: "RW", SA: "SA", SB: "SB", SC: "SC", SD: "SD", SE: "SE", SG: "SG", SH: "SH", SI: "SI", SJ: "SJ", SK: "SK", SL: "SL", SM: "SM", SN: "SN", SO: "SO", SR: "SR", ST: "ST", SV: "SV", SY: "SY", SZ: "SZ", TC: "TC", TD: "TD", TF: "TF", TG: "TG", TH: "TH", TJ: "TJ", TK: "TK", TL: "TL", TM: "TM", TN: "TN", TO: "TO", TR: "TR", TT: "TT", TV: "TV", TW: "TW", TZ: "TZ", UA: "UA", UG: "UG", UM: "UM", US: "US", UY: "UY", UZ: "UZ", VA: "VA", VC: "VC", VE: "VE", VG: "VG", VI: "VI", VN: "VN", VU: "VU", WF: "WF", WS: "WS", YE: "YE", YT: "YT", ZA: "ZA", ZM: "ZM", ZW: "ZW", // Official code for the unknown region. ZZ: "ZZ", }; var PhoneValidators = /** @class */ (function () { function PhoneValidators() { } PhoneValidators.checkRegionCode = function (local) { return !(regionsCode[local] === undefined); }; PhoneValidators.isValidRegionCode = function (control) { if (Util.isNotPresent(control)) { return undefined; } if (!PhoneValidators.checkRegionCode(control.value)) { return { noValidRegionCode: true }; } return undefined; }; PhoneValidators.isPhoneNumber = function (local) { var validator = function (control) { if (Util.isNotPresent(control)) { return undefined; } if (!PhoneValidators.checkRegionCode(local)) { return { noValidRegionCode: true }; } var phoneParser = googleLibphonenumber.PhoneNumberUtil.getInstance(); var error = { noPhoneNumber: true }; try { var phoneNumber = phoneParser.parse(control.value, local); if (phoneParser.isValidNumber(phoneNumber)) { error = undefined; } } catch (err) { error = { noPhoneNumber: true }; } return error; }; return validator; }; PhoneValidators.isPossibleNumberWithReason = function (local) { var validator = function (control) { if (Util.isNotPresent(control)) { return undefined; } if (!PhoneValidators.checkRegionCode(local)) { return { noValidRegionCode: true }; } var phoneParser = googleLibphonenumber.PhoneNumberUtil.getInstance(); var error = { noPhoneNumber: true }; try { var phoneNumber = phoneParser.parse(control.value, local); var reason = phoneParser.isPossibleNumberWithReason(phoneNumber); switch (reason) { case googleLibphonenumber.PhoneNumberUtil.ValidationResult.IS_POSSIBLE: error = undefined; break; case googleLibphonenumber.PhoneNumberUtil.ValidationResult.TOO_LONG: error = { phoneNumberTooLong: true }; break; case googleLibphonenumber.PhoneNumberUtil.ValidationResult.TOO_SHORT: error = { phoneNumberTooShort: true }; break; case googleLibphonenumber.PhoneNumberUtil.ValidationResult.INVALID_COUNTRY_CODE: error = { phoneNumberInvalidCountryCode: true }; break; default: error = { noPhoneNumber: true }; break; } } catch (err) { error = { noPhoneNumber: true }; } return error; }; return validator; }; return PhoneValidators; }()); var PossiblePhoneValidatorDirective = /** @class */ (function () { function PossiblePhoneValidatorDirective() { this.possiblePhone = "US"; } PossiblePhoneValidatorDirective.prototype.ngOnInit = function () { this.validator = PhoneValidators.isPossibleNumberWithReason(this.possiblePhone); }; PossiblePhoneValidatorDirective.prototype.validate = function (c) { return this.validator(c); }; return PossiblePhoneValidatorDirective; }()); PossiblePhoneValidatorDirective.decorators = [ { type: core.Directive, args: [{ selector: "[possiblePhone][formControlName],[possiblePhone][formControl],[possiblePhone][ngModel]", providers: [ { provide: forms.NG_VALIDATORS, // tslint:disable-next-line:no-forward-ref useExisting: core.forwardRef(function () { return PossiblePhoneValidatorDirective; }), multi: true, }, ], },] } ]; PossiblePhoneValidatorDirective.propDecorators = { possiblePhone: [{ type: core.Input }] }; var PhoneValidatorDirective = /** @class */ (function () { function PhoneValidatorDirective() { this.phone = "US"; } PhoneValidatorDirective.prototype.ngOnInit = function () { this.validator = PhoneValidators.isPhoneNumber(this.phone); }; PhoneValidatorDirective.prototype.validate = function (c) { return this.validator(c); }; return PhoneValidatorDirective; }()); PhoneValidatorDirective.decorators = [ { type: core.Directive, args: [{ selector: "[phone][formControlName],[phone][formControl],[phone][ngModel]", providers: [ { provide: forms.NG_VALIDATORS, // tslint:disable-next-line:no-forward-ref useExisting: core.forwardRef(function () { return PhoneValidatorDirective; }), multi: true, }, ], },] } ]; PhoneValidatorDirective.propDecorators = { phone: [{ type: core.Input }] }; var CountryCodeValidatorDirective = /** @class */ (function () { function CountryCodeValidatorDirective() { } CountryCodeValidatorDirective.prototype.ngOnInit = function () { this.validator = PhoneValidators.isValidRegionCode; }; CountryCodeValidatorDirective.prototype.validate = function (c) { return this.validator(c); }; return CountryCodeValidatorDirective; }()); CountryCodeValidatorDirective.decorators = [ { type: core.Directive, args: [{ selector: "[countryCode][formControlName],[countryCode][formControl],[countryCode][ngModel]", providers: [ { provide: forms.NG_VALIDATORS, // tslint:disable-next-line:no-forward-ref useExisting: core.forwardRef(function () { return CountryCodeValidatorDirective; }), multi: true, }, ], },] } ]; var PhoneValidatorsModule = /** @class */ (function () { function PhoneValidatorsModule() { } return PhoneValidatorsModule; }()); PhoneValidatorsModule.decorators = [ { type: core.NgModule, args: [{ declarations: [ PhoneValidatorDirective, PossiblePhoneValidatorDirective, CountryCodeValidatorDirective, ], exports: [ PhoneValidatorDirective, PossiblePhoneValidatorDirective, CountryCodeValidatorDirective, ], },] } ]; // validators /** * Generated bundle index. Do not edit. */ exports.CountryCodeValidatorDirective = CountryCodeValidatorDirective; exports.PhoneValidatorDirective = PhoneValidatorDirective; exports.PhoneValidators = PhoneValidators; exports.PhoneValidatorsModule = PhoneValidatorsModule; exports.PossiblePhoneValidatorDirective = PossiblePhoneValidatorDirective; exports.ɵa = regionsCode; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=ngx-phone-validators.umd.js.map