From e8ae23983b8bda0ef4c53ebaf24ae09a5ee4f10c Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Wed, 16 Dec 2020 17:02:58 +0000 Subject: [PATCH] [Library | Trunk]: String utils: Add url validator git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@60157 d315682c-612b-4755-9ff5-7f18f6832af3 --- utils/string-utils.class.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/utils/string-utils.class.ts b/utils/string-utils.class.ts index e8b135ff..04a1a77b 100644 --- a/utils/string-utils.class.ts +++ b/utils/string-utils.class.ts @@ -1,4 +1,5 @@ import {UrlSegment} from '@angular/router'; +import {ValidatorFn, Validators} from "@angular/forms"; export class Dates { public static yearMin = 1800; @@ -215,6 +216,12 @@ export class StringUtils { return !!email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"); } + public static urlValidator(): ValidatorFn { + return Validators.pattern('https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.' + + '[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.' + + '[a-zA-Z0-9]+\.[^\s]{2,}'); + } + public static sliceString(mystr, size: number): string { const sliced = String(mystr).substr(0, size); return sliced + (String(mystr).length > size ? '...' : '');