From a1c46c2648b3e75767b1d057f9eb573850b474db Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Wed, 28 Dec 2022 16:25:23 +0100 Subject: [PATCH] #8: Add URL check --- src/app/components/event/event.component.html | 5 ++-- src/app/components/event/event.component.ts | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/app/components/event/event.component.html b/src/app/components/event/event.component.html index 123f0db..3f390d7 100644 --- a/src/app/components/event/event.component.html +++ b/src/app/components/event/event.component.html @@ -22,10 +22,10 @@ - + {{event?.createdBy}} - + @@ -33,6 +33,7 @@

Start Date must not be after end date!

Required fields are missing!

Error creating event. Please try again.

+

Invalid URL, please only provide valid ones!

diff --git a/src/app/components/event/event.component.ts b/src/app/components/event/event.component.ts index 9fd14e6..c52ecdc 100644 --- a/src/app/components/event/event.component.ts +++ b/src/app/components/event/event.component.ts @@ -16,6 +16,7 @@ export class EventComponent implements OnInit { newEndDate: string | undefined; newEndTime: string | undefined; showDateError: boolean = false; + invalidUrlError: boolean = false; requiredFieldsMissing: boolean = false; showCreateError: boolean = false; @Output() deleteEvent = new EventEmitter(); @@ -53,6 +54,11 @@ export class EventComponent implements OnInit { return; } + if(!this.checkEventValidUrl()) { + this.invalidUrlError = true; + return; + } + if(this.event.eventId === undefined) { this.api.createEvent(this.event).subscribe((res: any) => { console.log(res); @@ -148,4 +154,27 @@ export class EventComponent implements OnInit { }); } } + + checkEventValidUrl(): boolean { + if(this.isNullOrBlank(this.event!.url)) { + return true; + } + + const urlRegex = /^(https?:\/\/)?((www\.)?([a-z0-9äöü]*\.)+[a-z]{2,10}.*)$/i; + + let validUrl = urlRegex.test(this.event!.url); + + if(validUrl){ + let match = this.event!.url.match(urlRegex); + if(match) { + console.log(match); + let url = 'https://' + match[2]; + this.event!.url = url; + } else { + validUrl = false; + } + } + + return validUrl; + } }