From a91558655c9452a1a2aacdc42611b9ce1f22f959 Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Tue, 2 Nov 2021 11:26:24 +0100 Subject: [PATCH] feat(StatusQ.Controls.Validators): introduce `StatusUrlValidator` --- .../Controls/Validators/StatusUrlValidator.qml | 15 +++++++++++++++ src/StatusQ/Controls/Validators/qmldir | 1 + src/StatusQ/Core/Utils/Utils.qml | 4 ++++ 3 files changed, 20 insertions(+) create mode 100644 src/StatusQ/Controls/Validators/StatusUrlValidator.qml diff --git a/src/StatusQ/Controls/Validators/StatusUrlValidator.qml b/src/StatusQ/Controls/Validators/StatusUrlValidator.qml new file mode 100644 index 00000000..dd808258 --- /dev/null +++ b/src/StatusQ/Controls/Validators/StatusUrlValidator.qml @@ -0,0 +1,15 @@ +import StatusQ.Core.Utils 0.1 +import StatusQ.Controls 0.1 + +StatusValidator { + + name: "url" + + errorMessage: "Please enter a valid URL" + + validate: function (value) { + return Utils.isURL(value); + } +} + + diff --git a/src/StatusQ/Controls/Validators/qmldir b/src/StatusQ/Controls/Validators/qmldir index fdca99cc..6299a5da 100644 --- a/src/StatusQ/Controls/Validators/qmldir +++ b/src/StatusQ/Controls/Validators/qmldir @@ -6,4 +6,5 @@ StatusAsyncValidator 0.1 StatusAsyncValidator.qml StatusAsyncEnsValidator 0.1 StatusAsyncEnsValidator.qml StatusMinLengthValidator 0.1 StatusMinLengthValidator.qml StatusMaxLengthValidator 0.1 StatusMaxLengthValidator.qml +StatusUrlValidator 0.1 StatusUrlValidator.qml StatusValidator 0.1 StatusValidator.qml diff --git a/src/StatusQ/Core/Utils/Utils.qml b/src/StatusQ/Core/Utils/Utils.qml index acd96367..f2be9e6b 100644 --- a/src/StatusQ/Core/Utils/Utils.qml +++ b/src/StatusQ/Core/Utils/Utils.qml @@ -27,6 +27,10 @@ QtObject { return isEmail || isDomain || (inputValue.startsWith("@") && inputValue.length > 1) } + function isURL(text) { + return (/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}(\.[a-zA-Z0-9()]{1,6})?\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/.test(text)) + } + function uuid() { return Date.now().toString(36) + Math.random().toString(36).substr(2, 5) }