From ff549d282249ca6875e33a3bbf12c11c559b805a Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Fri, 10 Sep 2021 12:51:16 +0200 Subject: [PATCH] feat(StatusQ.Controls.Validators): introduce `StatusAddressAndEnsValidator` --- .../Validators/StatusAddressOrEnsValidator.qml | 15 +++++++++++++++ ui/StatusQ/src/StatusQ/Controls/Validators/qmldir | 1 + ui/StatusQ/src/StatusQ/Core/Utils/Utils.qml | 8 ++++++++ 3 files changed, 24 insertions(+) create mode 100644 ui/StatusQ/src/StatusQ/Controls/Validators/StatusAddressOrEnsValidator.qml diff --git a/ui/StatusQ/src/StatusQ/Controls/Validators/StatusAddressOrEnsValidator.qml b/ui/StatusQ/src/StatusQ/Controls/Validators/StatusAddressOrEnsValidator.qml new file mode 100644 index 0000000000..45f7b424bb --- /dev/null +++ b/ui/StatusQ/src/StatusQ/Controls/Validators/StatusAddressOrEnsValidator.qml @@ -0,0 +1,15 @@ +import StatusQ.Controls 0.1 +import StatusQ.Core.Utils 0.1 + +StatusValidator { + name: "addressOrEns" + + errorMessage: "Please enter a valid address or ENS name." + + validate: function (t) { + return Utils.isValidAddress(t) || Utils.isValidEns(t) ? + true : + { actual: t } + } +} + diff --git a/ui/StatusQ/src/StatusQ/Controls/Validators/qmldir b/ui/StatusQ/src/StatusQ/Controls/Validators/qmldir index bd1db88cd7..0a8d026193 100644 --- a/ui/StatusQ/src/StatusQ/Controls/Validators/qmldir +++ b/ui/StatusQ/src/StatusQ/Controls/Validators/qmldir @@ -1,6 +1,7 @@ module StatusQ.Controls.Validators StatusAddressValidator 0.1 StatusAddressValidator.qml +StatusAddressOrEnsValidator 0.1 StatusAddressValidator.qml StatusValidator 0.1 StatusValidator.qml StatusMinLengthValidator 0.1 StatusMinLengthValidator.qml StatusMaxLengthValidator 0.1 StatusMaxLengthValidator.qml diff --git a/ui/StatusQ/src/StatusQ/Core/Utils/Utils.qml b/ui/StatusQ/src/StatusQ/Core/Utils/Utils.qml index 70de5ac6d6..2307e4ce59 100644 --- a/ui/StatusQ/src/StatusQ/Core/Utils/Utils.qml +++ b/ui/StatusQ/src/StatusQ/Core/Utils/Utils.qml @@ -18,6 +18,14 @@ QtObject { return inputValue !== "0x" && /^0x[a-fA-F0-9]{40}$/.test(inputValue) } + function isValidEns(inputValue) { + if (!inputValue) { + return false + } + const isEmail = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/.test(inputValue) + const isDomain = /(?:(?:(?[\w\-]*)(?:\.))?(?[\w\-]*))\.(?[\w\-]*)/.test(inputValue) + return isEmail || isDomain || (inputValue.startsWith("@") && inputValue.length > 1) + } }